jcs
/wikipedia
/amendments
/42
*: Remove xmalloc comments, handle malloc failure
jcs made amendment 42 about 1 year ago
--- browser.c Fri Sep 9 13:42:03 2022
+++ browser.c Wed Apr 5 12:10:03 2023
@@ -69,9 +69,13 @@ browser_idle(struct focusable *focusable, EventRecord
te = *(browser->input_te);
HLock(te->hText);
(*(te->hText))[te->teLength] = '\0';
- input = xstrdup(*(te->hText), "browser te input");
+ input = xstrdup(*(te->hText));
HUnlock(te->hText);
HUnlock(browser->input_te);
+ if (input == NULL) {
+ warn("Out of memory!");
+ break;
+ }
SetCursor(*(GetCursor(watchCursor)));
browser->wpr = wikipedia_fetch_article(browser, input);
@@ -124,7 +128,9 @@ browser_init(void)
Cell cell = { 0 };
short n, width, height;
- browser = xmalloczero(sizeof(struct browser), "browser");
+ browser = xmalloczero(sizeof(struct browser));
+ if (browser == NULL)
+ panic("Out of memory!");
browser->state = BROWSER_STATE_IDLE;
GetIndPattern(&fill_pattern, sysPatListID, 22);
@@ -144,7 +150,7 @@ browser_init(void)
browser->win = NewWindow(0L, &bounds, title, false, noGrowDocProc,
(WindowPtr)-1L, true, 0);
if (!browser->win)
- err(1, "Can't create window");
+ panic("Can't create window");
SetPort(browser->win);
/* search input TE */
@@ -157,6 +163,8 @@ browser_init(void)
TextFont(geneva);
TextSize(10);
browser->input_te = TENew(&te_bounds, &bounds);
+ if (browser->input_te == NULL)
+ panic("Out of memory!");
TEAutoView(true, browser->input_te);
TEActivate(browser->input_te);
@@ -175,6 +183,8 @@ browser_init(void)
te_bounds = bounds;
InsetRect(&te_bounds, 2, 2);
browser->debug_te = TENew(&te_bounds, &bounds);
+ if (browser->debug_te == NULL)
+ panic("Out of memory!");
TEAutoView(false, browser->debug_te);
(*(browser->debug_te))->caretHook = NullCaretHook;
TEActivate(browser->debug_te);
@@ -184,6 +194,8 @@ browser_init(void)
te_bounds = bounds;
InsetRect(&te_bounds, 2, 2);
browser->te = TEStylNew(&te_bounds, &bounds);
+ if (browser->te == NULL)
+ panic("Out of memory!");
TEAutoView(false, browser->te);
(*(browser->te))->caretHook = NullCaretHook;
TEActivate(browser->te);
@@ -200,7 +212,9 @@ browser_init(void)
UpdateScrollbarForTE(browser->win, browser->te_scroller,
browser->te, true);
- focusable = xmalloczero(sizeof(struct focusable), "focusable");
+ focusable = xmalloczero(sizeof(struct focusable));
+ if (focusable == NULL)
+ panic("Out of memory!");
focusable->cookie = browser;
focusable->win = browser->win;
focusable->idle = browser_idle;
@@ -328,6 +342,7 @@ void
browser_mouse_down(struct focusable *focusable, EventRecord *event)
{
struct browser *browser = (struct browser *)focusable->cookie;
+ struct browser_link *link;
char str[255];
Cell selected = { 0 };
Point p;
@@ -376,7 +391,8 @@ browser_mouse_down(struct focusable *focusable, EventR
r = (*(browser->debug_te))->viewRect;
HUnlock(browser->debug_te);
if (PtInRect(p, &r)) {
- TEClick(p, ((event->modifiers & shiftKey) != 0), browser->debug_te);
+ TEClick(p, ((event->modifiers & shiftKey) != 0),
+ browser->debug_te);
browser_update_menu(browser);
return;
}
@@ -389,8 +405,7 @@ browser_mouse_down(struct focusable *focusable, EventR
off = TEGetOffset(p, browser->te);
for (n = 0; n < browser->links_count; n++) {
- struct browser_link *link = &browser->links[n];
-
+ link = &browser->links[n];
if ((link->pos <= off) && (off < link->pos + link->len)) {
TESetText(link->link, strlen(link->link), browser->input_te);
HLock(browser->input_te);
@@ -484,9 +499,13 @@ browser_live_search(struct browser *browser)
HLock(te->hText);
(*(te->hText))[te->teLength] = '\0';
- input = xstrdup(*(te->hText), "browser te input");
+ input = xstrdup(*(te->hText));
HUnlock(te->hText);
HUnlock(browser->input_te);
+ if (input == NULL) {
+ warn("Out of memory!");
+ return;
+ }
nresults = wikipedia_fetch_search_results(browser, input, &results);
xfree(&input);
@@ -579,14 +598,17 @@ browser_handle_menu(struct focusable *focusable, short
HLock(browser->debug_te);
HLock(browser->te);
- SetCtlValue(browser->te_scroller, GetCtlMin(browser->te_scroller));
+ SetCtlValue(browser->te_scroller,
+ GetCtlMin(browser->te_scroller));
if (browser_debug_enabled(browser)) {
/* disable debugging */
SetItemMark(view_menu, VIEW_MENU_DEBUG_ID, noMark);
- (*(browser->debug_te))->destRect = (*(browser->te))->destRect;
- (*(browser->debug_te))->viewRect = (*(browser->te))->viewRect;
+ (*(browser->debug_te))->destRect =
+ (*(browser->te))->destRect;
+ (*(browser->debug_te))->viewRect =
+ (*(browser->te))->viewRect;
(*(browser->te))->destRect = te_bounds;
(*(browser->te))->viewRect = bounds;
@@ -598,13 +620,16 @@ browser_handle_menu(struct focusable *focusable, short
/* enable debugging */
SetItemMark(view_menu, VIEW_MENU_DEBUG_ID, checkMark);
- (*(browser->te))->destRect = (*(browser->debug_te))->destRect;
- (*(browser->te))->viewRect = (*(browser->debug_te))->viewRect;
+ (*(browser->te))->destRect =
+ (*(browser->debug_te))->destRect;
+ (*(browser->te))->viewRect =
+ (*(browser->debug_te))->viewRect;
(*(browser->debug_te))->destRect = te_bounds;
(*(browser->debug_te))->viewRect = bounds;
EraseRect(&(*(browser->debug_te))->destRect);
- TEUpdate(&(*(browser->debug_te))->destRect, browser->debug_te);
+ TEUpdate(&(*(browser->debug_te))->destRect,
+ browser->debug_te);
UpdateScrollbarForTE(browser->win, browser->te_scroller,
browser->debug_te, false);
}
@@ -799,12 +824,20 @@ browser_print(struct browser *browser, const char *str
/* Atari 2600 -> Atari 2600 */
for (n = 0; n <= len; n++) {
if (n == len) {
- link->link = xstrndup(str, n, "link");
+ link->link = xstrndup(str, n);
+ if (link->link == NULL) {
+ warn("Out of memory");
+ return 0;
+ }
break;
}
if (str[n] == '|') {
- link->link = xstrndup(str, n, "link");
+ link->link = xstrndup(str, n);
+ if (link->link == NULL) {
+ warn("Out of memory");
+ return 0;
+ }
str += n + 1;
len -= n + 1;
break;
@@ -896,7 +929,8 @@ browser_draw_line(struct browser *browser)
TextSize(cursize);
HLock(browser->te);
- lsize = (*(browser->te))->viewRect.right - (*(browser->te))->viewRect.left;
+ lsize = (*(browser->te))->viewRect.right -
+ (*(browser->te))->viewRect.left;
HUnlock(browser->te);
lsize /= cwidth;
--- http.c Wed Sep 7 15:27:25 2022
+++ http.c Wed Apr 5 12:16:49 2023
@@ -31,9 +31,24 @@ url_parse(const char *str)
size_t len, schemelen, hostlen, pathlen;
len = strlen(str);
- scheme = xmalloc(len + 1, "url_parse scheme");
- host = xmalloc(len + 1, "url_parse host");
- path = xmalloc(len + 1, "url_parse path");
+ scheme = xmalloc(len + 1);
+ if (scheme == NULL) {
+ warn("http: Failed allocating %ld", len + 1);
+ return NULL;
+ }
+ host = xmalloc(len + 1);
+ if (host == NULL) {
+ warn("http: Failed allocating %ld", len + 1);
+ xfree(&scheme);
+ return NULL;
+ }
+ path = xmalloc(len + 1);
+ if (path == NULL) {
+ warn("http: Failed allocating %ld", len + 1);
+ xfree(&host);
+ xfree(&scheme);
+ return NULL;
+ }
/* scheme://host:port/path */
ret = sscanf(str, "%[^:]://%[^:]:%d%s%n", scheme, host, &port, path,
@@ -70,7 +85,11 @@ consolidate:
* free(url)
*/
len = sizeof(struct url) + schemelen + 1 + hostlen + 1 + pathlen + 1;
- url = xmalloc(len, "url");
+ url = xmalloc(len);
+ if (url == NULL) {
+ warn("http: Failed allocating %ld for URL", len);
+ goto cleanup;
+ }
url->scheme = (char *)url + sizeof(struct url);
len = strlcpy(url->scheme, scheme, schemelen + 1);
@@ -122,7 +141,11 @@ encode:
return ret;
}
- ret = xmalloc(len + 1, "url_encode");
+ ret = xmalloc(len + 1);
+ if (ret == NULL) {
+ warn("http: Failed allocating %ld", len + 1);
+ return NULL;
+ }
len = 0;
goto encode;
}
@@ -142,10 +165,19 @@ http_get(const char *surl)
if (url == NULL)
return NULL;
- req = xmalloczero(sizeof(struct http_request), "http_get");
+ req = xmalloczero(sizeof(struct http_request));
+ if (req == NULL) {
+ warn("http: Failed allocating http_request");
+ return NULL;
+ }
req->url = url;
req->tcp_buf_size = (4 * 1500) + sizeof(req->chunk);
- req->tcp_buf = xmalloc(req->tcp_buf_size, "http_get buf");
+ req->tcp_buf = xmalloc(req->tcp_buf_size);
+ if (req->tcp_buf == NULL) {
+ warn("http: Failed allocating tcp_buf");
+ xfree(&req);
+ return NULL;
+ }
err = _TCPCreate(&req->tcp_iopb, &req->tcp_stream, (Ptr)req->tcp_buf,
req->tcp_buf_size, nil, nil, nil, false);
@@ -154,7 +186,7 @@ http_get(const char *surl)
goto error;
}
- err = ResolveName(req->url->host, &req->host_ip);
+ err = DNSResolveName(&req->url->host, &req->host_ip, NULL);
if (err) {
warn("Couldn't resolve host %s (%d)", req->url->host, err);
goto error;
@@ -179,7 +211,11 @@ http_get(const char *surl)
}
alen = 256 + strlen(req->url->host) + strlen(req->url->path);
- req->message = xmalloc(alen, "http_get verb");
+ req->message = xmalloc(alen);
+ if (req->message == NULL) {
+ warn("http: Failed allocating message");
+ goto error;
+ }
len = snprintf(req->message, alen,
"GET %s HTTP/1.0\r\n"
"Host: %s\r\n"
@@ -315,9 +351,9 @@ http_req_free(void *reqptr)
_TCPRelease(&req->tcp_iopb, req->tcp_stream, nil, nil, false);
-// if (req->message != NULL)
-// xfree(&req->message);
-// xfree(&req->tcp_buf);
-// xfree(&req->url);
-// xfree(reqptr);
+ if (req->message != NULL)
+ xfree(&req->message);
+ xfree(&req->tcp_buf);
+ xfree(&req->url);
+ xfree(&req);
}
--- utf8.c Wed Sep 7 15:41:18 2022
+++ utf8.c Wed Apr 5 10:42:26 2023
@@ -111,7 +111,11 @@ macroman_to_utf8_string(unsigned char *str, size_t len
size_t ulen, n;
const utf8_char *u;
- tmp = xmalloc((len * 4) + 1, "macroman_to_utf8 tmp");
+ tmp = xmalloc((len * 4) + 1);
+ if (tmp == NULL) {
+ warn("utf8: Failed allocating (%ld * 4) + 1", len);
+ return NULL;
+ }
ulen = 0;
for (n = 0; n < len; n++) {
@@ -143,9 +147,11 @@ macroman_to_utf8_string(unsigned char *str, size_t len
}
tmp[ulen] = '\0';
- ret = (unsigned char *)xstrdup((char *)tmp,
- "macroman_to_utf8_string");
+ ret = (unsigned char *)xstrdup((char *)tmp);
xfree(&tmp);
+
+ if (ret == NULL)
+ warn("utf8: Out of memory!");
return ret;
}
--- wikipedia.c Fri Sep 9 13:33:38 2022
+++ wikipedia.c Wed Apr 5 11:32:30 2023
@@ -37,22 +37,49 @@ wikipedia_fetch_article(struct browser *browser, char
progress("Fetching article \"%s\"...", name);
- wpr = xmalloczero(sizeof(struct wikipedia_request),
- "fetch_article wpr");
+ wpr = xmalloczero(sizeof(struct wikipedia_request));
+ if (wpr == NULL) {
+ progress(NULL);
+ warn("Out of memory!");
+ return NULL;
+ }
wpr->browser = browser;
uname = macroman_to_utf8_string((unsigned char *)name, strlen(name));
+ if (uname == NULL) {
+ progress(NULL);
+ xfree(&wpr);
+ return NULL;
+ }
nencoded = url_encode(uname);
xfree(&uname);
+ if (nencoded == NULL) {
+ progress(NULL);
+ xfree(&wpr);
+ return NULL;
+ }
snprintf(url, sizeof(url), "http://%s/w/api.php?action=query&"
"prop=revisions&rvslots=*&rvprop=content&"
"format=xml&titles=%s", WIKIPEDIA_HOST, nencoded);
xfree(&nencoded);
wpr->http_request = http_get(url);
+ if (wpr->http_request == NULL) {
+ progress(NULL);
+ xfree(&nencoded);
+ xfree(&wpr);
+ return NULL;
+ }
http_req_skip_header(wpr->http_request);
wpr->state = WP_STATE_XML_INIT;
- wpr->normalized_title = xstrdup(name, "normalized_title");
+ wpr->normalized_title = xstrdup(name);
+ if (wpr->normalized_title == NULL) {
+ progress(NULL);
+ warn("Out of memory!");
+ xfree(&nencoded);
+ xfree(&wpr);
+ return NULL;
+ }
browser_debug_print(wpr->browser, wpr->http_request->chunk,
wpr->http_request->chunk_len);
@@ -62,12 +89,12 @@ wikipedia_fetch_article(struct browser *browser, char
size_t
wikipedia_fetch_search_results(struct browser *browser, char *query,
- char ***results)
+ char ***results) /* a triple-star program! */
{
static char url[256];
struct http_request *req;
char *qencoded;
- char **rets = NULL;
+ char **rets = NULL, **trets = NULL;
char *str = NULL, *nstr = NULL, c;
unsigned char *uquery;
short strings = 0;
@@ -82,21 +109,33 @@ wikipedia_fetch_search_results(struct browser *browser
size_t buf_size;
size_t buf_len;
- uquery = macroman_to_utf8_string((unsigned char *)query, strlen(query));
+ uquery = macroman_to_utf8_string((unsigned char *)query,
+ strlen(query));
+ if (uquery == NULL)
+ return 0;
qencoded = url_encode(uquery);
xfree(&uquery);
-
+ if (qencoded == NULL)
+ return 0;
+
snprintf(url, sizeof(url), "http://%s/w/api.php?action=opensearch&"
"format=xml&namespace=0&limit=10&redirects=return&search=%s",
WIKIPEDIA_HOST, qencoded);
xfree(&qencoded);
req = http_get(url);
+ if (req == NULL)
+ return 0;
http_req_skip_header(req);
buf_size = 256;
buf_len = 0;
- buf = xmalloc(buf_size, "xml buf");
-
+ buf = xmalloc(buf_size);
+ if (buf == NULL) {
+ warn("Out of memory!");
+ http_req_free(&req);
+ return 0;
+ }
+
for (;;) {
if (req->chunk_len == 0 || (req->chunk_off + 1 > req->chunk_len)) {
req->chunk_len = http_req_read(req, req->chunk,
@@ -112,8 +151,17 @@ wikipedia_fetch_search_results(struct browser *browser
if (c == '<') {
if (xstate == XML_IN_TEXT) {
nrets++;
- rets = xreallocarray(rets, sizeof(Ptr), nrets);
- nstr = xstrndup(buf, buf_len, "search result");
+ trets = xreallocarray(rets, sizeof(Ptr), nrets);
+ if (trets == NULL) {
+ warn("Out of memory!");
+ break;
+ }
+ rets = trets;
+ nstr = xstrndup(buf, buf_len);
+ if (nstr == NULL) {
+ warn("Out of memory!");
+ break;
+ }
rets[nrets - 1] = nstr;
}
@@ -157,7 +205,7 @@ wikipedia_fetch_search_results(struct browser *browser
}
http_req_free(&req);
- http_req_free(&buf);
+ xfree(&buf);
*results = rets;
@@ -208,7 +256,12 @@ get_char:
case WP_STATE_XML_INIT:
wpr->buf_size = 1024;
wpr->buf_len = 0;
- wpr->buf = xmalloc(wpr->buf_size, "wpr buf");
+ wpr->buf = xmalloc(wpr->buf_size);
+ if (wpr->buf == NULL) {
+ warn("Out of memory!");
+ wpr->state = WP_STATE_DONE;
+ break;
+ }
wpr->state = WP_STATE_XML_PARSE;
goto get_char;
@@ -235,8 +288,11 @@ get_char:
count > 10) {
if (wpr->normalized_title != NULL)
xfree(&wpr->normalized_title);
- wpr->normalized_title = xstrdup(to_normalized,
- "to_normalized");
+ wpr->normalized_title = xstrdup(to_normalized);
+ if (wpr->normalized_title == NULL) {
+ warn("Out of memory!");
+ goto done_parsing;
+ }
} else
xstate = XML_DEFAULT;
}
@@ -415,13 +471,25 @@ get_char:
if (strncmp(wpr->buf, "convert|", 8) == 0) {
/* convert|5.1|lb|... */
/* convert|9|in|cm|adj=on */
- char *conv = xmalloc(wpr->buf_len, "convert");
- char *conv2 = xmalloc(wpr->buf_len, "convert");
+ char *conv, *conv2;
size_t len;
+
+ conv = xmalloc(wpr->buf_len);
+ if (conv == NULL) {
+ warn("Failed allocating %ld", wpr->buf_len);
+ break;
+ }
+ conv2 = xmalloc(wpr->buf_len);
+ if (conv2 == NULL) {
+ warn("Failed allocating %ld", wpr->buf_len);
+ xfree(&conv);
+ break;
+ }
wpr->buf[wpr->buf_len] = '\0';
- if (sscanf(wpr->buf, "convert|%[^|]|%[^|]|%n", conv, conv2,
- &len) == 2 && len >= 13)
- wpr->buf_len = sprintf(wpr->buf, "%s %s ", conv, conv2);
+ if (sscanf(wpr->buf, "convert|%[^|]|%[^|]|%n", conv,
+ conv2, &len) == 2 && len >= 13)
+ wpr->buf_len = snprintf(wpr->buf, wpr->buf_size,
+ "%s %s ", conv, conv2);
else
wpr->buf_len = 0;
xfree(&conv);
@@ -450,13 +518,16 @@ get_char:
wpr->buf_len = 0;
wpr->redirect = true;
} else if (wpr->redirect &&
- !(wpr->style & STYLE_LINK) &&
- (wpr->last_style & STYLE_LINK)) {
+ !(wpr->style & STYLE_LINK) && (wpr->last_style & STYLE_LINK)) {
if (wpr->normalized_title)
xfree(&wpr->normalized_title);
wpr->buf[wpr->buf_len] = '\0';
- wpr->normalized_title = xstrdup(wpr->buf, "title");
- wpr->state = WP_STATE_HAVE_REDIRECT;
+ wpr->normalized_title = xstrdup(wpr->buf);
+ if (wpr->normalized_title == NULL) {
+ warn("Out of memory!");
+ wpr->state = WP_STATE_DONE;
+ } else
+ wpr->state = WP_STATE_HAVE_REDIRECT;
goto done_parsing;
}