on master help sign in
Check all the results of malloc, spotted by Visual Studio
commitae35534054dd5d31bc4ac49af5f02e2b59a33153 authorNeil Mitchell <ndmitchell@gmail.com> dateWed 30 Nov 2016, 19:08 (10 years ago) parent1c72d10
2 files changed+6
cbits/hexml.c+4 vs/main.c+2
cbits/hexml.c+4
@@ -291,6 +291,7 @@ void static inline node_alloc(node_buffer* b, int ask)
291291 if (space >= ask) return;
292292 int size2 = (b->size + 1000 + ask) * 2;
293293 node* buf2 = malloc(size2 * sizeof(node));
294+ assert(buf2);
294295 memcpy(buf2, b->nodes, b->used_front * sizeof(node));
295296 memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
296297 free(b->alloc);
@@ -305,6 +306,7 @@ void static inline attr_alloc(attr_buffer* b, int ask)
305306 if (space >= ask) return;
306307 int size2 = (b->size + 1000 + ask) * 2;
307308 attr* buf2 = malloc(size2 * sizeof(attr));
309+ assert(buf2);
308310 memcpy(buf2, b->attrs, b->used * sizeof(attr));
309311 free(b->alloc);
310312 b->size = size2;
@@ -316,6 +318,7 @@ void set_error(document* d, char* msg)
316318{
317319 if (d->error_message != NULL) return; // keep the first error message
318320 d->error_message = malloc(strlen(msg)+1);
321+ assert(d->error_message);
319322 strcpy(d->error_message, msg);
320323}
321324
@@ -511,6 +514,7 @@ document* document_parse(char* s, int slen)
511514 init_parse_table();
512515
513516 buffer* buf = malloc(sizeof(buffer));
517+ assert(buf);
514518 document* d = &buf->document;
515519 d->body = s;
516520 d->cursor = s;
vs/main.c+2
@@ -12,6 +12,7 @@ char* readFile(char* file)
1212 int len = ftell(f);
1313 fseek(f, 0, SEEK_SET);
1414 char* res = malloc(len + 1);
15+ assert(res);
1516 res[len] = 0;
1617 size_t n = fread(res, 1, len, f);
1718 assert(n == len);
@@ -31,6 +32,7 @@ void main(int argc, char **argv)
3132 {
3233 int len = node_render(doc, document_node(doc), NULL, 0);
3334 char* s = malloc(len + 1);
35+ assert(s);
3436 node_render(doc, document_node(doc), s, len);
3537 s[len] = 0;
3638 printf("Parse successful\n"); // , %s\n", s);