jcs
/textview
/amendments
/24
Better error paths, fix signedness issue with width caches
jcs made amendment 24 over 2 years ago
--- textview.c Thu Oct 17 17:52:01 2024
+++ textview.c Thu Oct 17 23:03:19 2024
@@ -151,15 +151,18 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
unsigned long avail;
short width, prev_style_idx;
char *text_slab_data;
- bool new_chunk = false, ret = true;
-
+ bool new_chunk, ret;
+
tv = TVLock(tvh);
+ new_chunk = false;
+ ret = false;
+
/* setup style */
prev_style_idx = tv->cur_style;
cur_style = TVFindStyle(tv, style);
if (cur_style == NULL)
- return false;
+ goto append_done;
if (tv->cur_style != prev_style_idx)
new_chunk = true;
@@ -175,11 +178,8 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
new_chunk = false;
prev_chunk = chunk;
chunk = TVCreateChunk(tv);
- if (chunk == NULL) {
- TVRevealCanvas(tv);
- TVUnlock(tv);
- return false;
- }
+ if (chunk == NULL)
+ goto append_done;
chunk->start = text_slab->base + text_slab->pos;
chunk->end = chunk->start;
@@ -195,10 +195,8 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
/* new text slab, new chunk */
chunk->end = text_slab->base + text_slab->pos;
- if ((text_slab = TVAllocateTextSlab(tv)) == NULL) {
- ret = false;
- break;
- }
+ if ((text_slab = TVAllocateTextSlab(tv)) == NULL)
+ goto append_done;
new_chunk = true;
continue;
@@ -217,11 +215,12 @@ TVAppend(TVHandle tvh, struct TVStyle *style, char *te
chunk->dirty = true;
}
- if (TVCalcLines(tv))
+ if (TVCalcLines(tv)) {
TVDrawVisibleLines(tv);
- else
- ret = false;
+ ret = true;
+ }
+append_done:
TVUnlock(tv);
return ret;
}
@@ -256,6 +255,8 @@ TVUpdateScrollbar(TVHandle tvh, ControlHandle scroller
(*scroller)->contrlMin = 0;
max = tv->bounds.bottom - tv->bounds.top -
(tv->view.bottom - tv->view.top);
+ if (max > SHRT_MAX)
+ Debugger();
(*scroller)->contrlMax = (max > 0 ? max : 0);
SetCtlValue(scroller, -(tv->scrolled.top));
HUnlock(scroller);
@@ -799,7 +800,8 @@ TVCalcLines(struct TVRec *tv)
unsigned long avail, start, linen, last_space, max_right;
long n, back_width, back_n;
short width, space_width, height;
- char c, tc, *text_slab_data;
+ char *text_slab_data;
+ unsigned char c;
bool new_line;
/* find first dirty chunk */
@@ -863,12 +865,13 @@ TVCalcLines(struct TVRec *tv)
else
start = prev_line->end;
}
-
+
new_line = false;
last_space = 0;
height = style->font_info.ascent + style->font_info.descent;
for (n = start; n < chunk->end; n++) {
+ if (n == 38736) Debugger();
c = text_slab_data[n - text_slab->base];
if (c == '\r') {
@@ -973,6 +976,7 @@ TVDrawVisibleLines(struct TVRec *tv)
unsigned long bottom, start, end, len;
unsigned short chars, linen;
long left;
+ bool ret;
TVUseCanvas(tv);
EraseRect(&tv->canvas->portRect);
@@ -980,11 +984,12 @@ TVDrawVisibleLines(struct TVRec *tv)
tv->top_line = 0;
tv->bottom_line = 0;
chunk = NULL;
+ ret = false;
for (linen = 1; linen <= tv->nlines; linen++) {
line = TVGetLine(tv, linen);
if (line == NULL)
- return false;
+ goto draw_done;
if (tv->scrolled.top + line->rect.bottom < 0L)
/* is not in the view at all yet */
@@ -999,23 +1004,23 @@ TVDrawVisibleLines(struct TVRec *tv)
if (chunk == NULL) {
chunk = TVGetChunk(tv, tv->first_chunk);
if (chunk == NULL)
- return false;
+ goto draw_done;
} else {
while (chunk->end_line < linen) {
chunk = TVGetChunk(tv, chunk->next_chunk);
if (!chunk)
- return false;
+ goto draw_done;
}
}
text_slab = TVGetTextSlab(tv, chunk->start);
if (text_slab == NULL)
- return false;
+ goto draw_done;
text_slab_data = *(text_slab->data);
style = &tv->styles[chunk->style];
if (style == NULL)
- return false;
+ goto draw_done;
TVApplyStyle(style);
}
@@ -1049,9 +1054,11 @@ TVDrawVisibleLines(struct TVRec *tv)
DrawText(text_slab_data + (start - text_slab->base), 0,
end - start);
}
-
+ ret = true;
+
+draw_done:
TVRevealCanvas(tv);
- return true;
+ return ret;
}
void