Changeset: 53ab0ebe7937 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=53ab0ebe7937 Modified Files: clients/mapilib/mapi.c clients/odbc/driver/ODBCDesc.c common/stream/stream.c Branch: Jul2017 Log Message:
realloc error handling. diffs (237 lines): diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c --- a/clients/mapilib/mapi.c +++ b/clients/mapilib/mapi.c @@ -988,7 +988,16 @@ static int mapi_initialized = 0; return (e); \ } \ } while (0) -#define REALLOC(p,c) ((p) = ((p) ? realloc((p),(c)*sizeof(*(p))) : malloc((c)*sizeof(*(p))))) +#define REALLOC(p, c) \ + do { \ + if (p) { \ + void *tmp = (p); \ + (p) = realloc((p), (c) * sizeof(*(p))); \ + if ((p) == NULL) \ + free(tmp); \ + } else \ + (p) = malloc((c) * sizeof(*(p))); \ + } while (0) /* * Blocking @@ -1009,11 +1018,13 @@ static int mapi_initialized = 0; * errors, and mapi_explain or mapi_explain_query to print a formatted error * report. */ +static char nomem[] = "Memory allocation failed"; + static void mapi_clrError(Mapi mid) { assert(mid); - if (mid->errorstr) + if (mid->errorstr && mid->errorstr != nomem) free(mid->errorstr); mid->action = 0; /* contains references to constants */ mid->error = 0; @@ -1025,7 +1036,10 @@ mapi_setError(Mapi mid, const char *msg, { assert(msg); REALLOC(mid->errorstr, strlen(msg) + 1); - strcpy(mid->errorstr, msg); + if (mid->errorstr == NULL) + mid->errorstr = nomem; + else + strcpy(mid->errorstr, msg); mid->error = error; mid->action = action; return mid->error; @@ -1592,7 +1606,7 @@ close_result(MapiHdl hdl) result->cache.line = NULL; result->cache.tuplecount = 0; } - if (result->errorstr) + if (result->errorstr && result->errorstr != nomem) free(result->errorstr); result->errorstr = NULL; result->hdl = NULL; @@ -1611,8 +1625,12 @@ add_error(struct MapiResultSet *result, size_t size = result->errorstr ? strlen(result->errorstr) : 0; REALLOC(result->errorstr, size + strlen(error) + 2); - strcpy(result->errorstr + size, error); - strcat(result->errorstr + size, "\n"); + if (result->errorstr == NULL) + result->errorstr = nomem; + else { + strcpy(result->errorstr + size, error); + strcat(result->errorstr + size, "\n"); + } } char * @@ -2126,7 +2144,7 @@ mapi_destroy(Mapi mid) (void) mapi_disconnect(mid); if (mid->blk.buf) free(mid->blk.buf); - if (mid->errorstr) + if (mid->errorstr && mid->errorstr != nomem) free(mid->errorstr); if (mid->hostname) free(mid->hostname); @@ -2766,7 +2784,8 @@ mapi_reconnect(Mapi mid) mid->errorstr = NULL; mapi_close_handle(hdl); mapi_setError(mid, errorstr, "mapi_reconnect", error); - free(errorstr); /* now free it after a copy has been made */ + if (errorstr != nomem) + free(errorstr); /* now free it after a copy has been made */ close_connection(mid); return mid->error; } @@ -3208,9 +3227,14 @@ mapi_prepare(Mapi mid, const char *cmd) do { \ /* note: k==strlen(hdl->query) */ \ if (k+len >= lim) { \ + char *q = hdl->query; \ lim = k + len + MAPIBLKSIZE; \ hdl->query = realloc(hdl->query, lim); \ - assert(hdl->query); \ + if (hdl->query == NULL) { \ + free(q); \ + return; \ + } \ + hdl->query = q; \ } \ } while (0) @@ -3228,7 +3252,8 @@ mapi_param_store(MapiHdl hdl) lim = strlen(hdl->template) + MAPIBLKSIZE; REALLOC(hdl->query, lim); - assert(hdl->query); + if (hdl->query == NULL) + return; hdl->query[0] = 0; k = 0; @@ -3244,7 +3269,8 @@ mapi_param_store(MapiHdl hdl) if (k + (q - p) >= lim) { lim += MAPIBLKSIZE; REALLOC(hdl->query, lim); - assert(hdl->query); + if (hdl->query == NULL) + return; } strncpy(hdl->query + k, p, q - p); k += q - p; diff --git a/clients/odbc/driver/ODBCDesc.c b/clients/odbc/driver/ODBCDesc.c --- a/clients/odbc/driver/ODBCDesc.c +++ b/clients/odbc/driver/ODBCDesc.c @@ -163,10 +163,14 @@ setODBCDescRecCount(ODBCDesc *desc, int desc->descRec = NULL; } else if (desc->descRec == NULL) { assert(desc->sql_desc_count == 0); - desc->descRec = (ODBCDescRec *) malloc((count + 1) * sizeof(*desc->descRec)); + desc->descRec = malloc((count + 1) * sizeof(*desc->descRec)); } else { + ODBCDescRec *p; assert(desc->sql_desc_count > 0); - desc->descRec = (ODBCDescRec *) realloc(desc->descRec, (count + 1) * sizeof(*desc->descRec)); + p = realloc(desc->descRec, (count + 1) * sizeof(*desc->descRec)); + if (p == NULL) + return; /* TODO: error handling */ + desc->descRec = p; } if (count > desc->sql_desc_count) { int i; diff --git a/common/stream/stream.c b/common/stream/stream.c --- a/common/stream/stream.c +++ b/common/stream/stream.c @@ -3237,7 +3237,7 @@ ic_flush(stream *s) if (ic->buflen > 0 || iconv(ic->cd, NULL, NULL, &outbuf, &outbytesleft) == (size_t) -1 || (outbytesleft < sizeof(ic->buffer) && - mnstr_write(ic->s, ic->buffer, 1, sizeof(ic->buffer) - outbytesleft) < 0)) { + mnstr_write(ic->s, ic->buffer, 1, sizeof(ic->buffer) - outbytesleft) < 0)) { s->errnr = MNSTR_WRITE_ERROR; return -1; } @@ -3448,8 +3448,13 @@ buffer_get_buf(buffer *b) if (b == NULL) return NULL; - if (b->pos == b->len && (b->buf = realloc(b->buf, b->len + 1)) == NULL) - return NULL; + if (b->pos == b->len) { + if ((r = realloc(b->buf, b->len + 1)) == NULL) { + /* keep b->buf in tact */ + return NULL; + } + b->buf = r; + } r = b->buf; r[b->pos] = '\0'; b->buf = malloc(b->len); @@ -3505,14 +3510,14 @@ buffer_write(stream *s, const void *buf, return -1; } if (b->pos + size > b->len) { - size_t ns = b->len; - - while (b->pos + size > ns) - ns *= 2; - if ((b->buf = realloc(b->buf, ns)) == NULL) { + char *p; + size_t ns = b->pos + size + 8192; + + if ((p = realloc(b->buf, ns)) == NULL) { s->errnr = MNSTR_WRITE_ERROR; return -1; } + b->buf = p; b->len = ns; } memcpy(b->buf + b->pos, buf, size); @@ -5076,12 +5081,14 @@ bstream_read(bstream *s, size_t size) } assert(s->buf != NULL); - if (s->len == s->size && - (s->buf = realloc(s->buf, (s->size <<= 1) + 1)) == NULL) { - s->size = 0; - s->len = 0; - s->pos = 0; - return -1; + if (s->len == s->size) { + char *p; + size_t ns = s->size + size + 8192; + if ((p = realloc(s->buf, ns + 1)) == NULL) { + return -1; + } + s->size = ns; + s->buf = p; } if (size > s->size - s->len) @@ -5127,12 +5134,14 @@ bstream_readline(bstream *s) } assert(s->buf != NULL); - if (s->len == s->size && - (s->buf = realloc(s->buf, (s->size <<= 1) + 1)) == NULL) { - s->size = 0; - s->len = 0; - s->pos = 0; - return -1; + if (s->len == s->size) { + char *p; + size_t ns = s->size + size + 8192; + if ((p = realloc(s->buf, ns + 1)) == NULL) { + return -1; + } + s->size = ns; + s->buf = p; } if (size > s->size - s->len) _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list