From c9f279ea28dbc7d9f1c8ed109f01a869994dc297 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Fri, 19 Aug 2011 01:46:16 +0900 Subject: [PATCH] checking for OOM in sexp_read_symbol --- sexp.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sexp.c b/sexp.c index 163bdaed..1cc2e2d4 100644 --- a/sexp.c +++ b/sexp.c @@ -1578,7 +1578,7 @@ sexp sexp_read_symbol (sexp ctx, sexp in, int init, int internp) { int c, i=0, size=INIT_STRING_BUFFER_SIZE; char initbuf[INIT_STRING_BUFFER_SIZE]; char *buf=initbuf, *tmp; - sexp res; + sexp res=SEXP_VOID; #if SEXP_USE_FOLD_CASE_SYMS int foldp = sexp_port_fold_casep(in); init = (foldp ? tolower(init) : init); @@ -1599,6 +1599,7 @@ sexp sexp_read_symbol (sexp ctx, sexp in, int init, int internp) { buf[i++] = c; if (i >= size) { /* expand buffer w/ malloc(), later free() it */ tmp = (char*) sexp_malloc(size*2); + if (!tmp) {res = sexp_global(ctx, SEXP_G_OOM_ERROR); break;} memcpy(tmp, buf, i); if (size != INIT_STRING_BUFFER_SIZE) free(buf); buf = tmp; @@ -1606,8 +1607,10 @@ sexp sexp_read_symbol (sexp ctx, sexp in, int init, int internp) { } } - buf[i] = '\0'; - res = (internp ? sexp_intern(ctx, buf, i) : sexp_c_string(ctx, buf, i)); + if (!sexp_exceptionp(res)) { + buf[i] = '\0'; + res = (internp ? sexp_intern(ctx, buf, i) : sexp_c_string(ctx, buf, i)); + } if (size != INIT_STRING_BUFFER_SIZE) free(buf); return res; }