on master help sign in
Check all the results of malloc, spotted by Visual Studio
ae35534Neil Mitchell
cbits/hexml.c+4
@@ -291,6 +291,7 @@ void static inline node_alloc(node_buffer* b, int ask)
291 if (space >= ask) return;
291 if (space >= ask) return;
292 int size2 = (b->size + 1000 + ask) * 2;
292 int size2 = (b->size + 1000 + ask) * 2;
293 node* buf2 = malloc(size2 * sizeof(node));
293 node* buf2 = malloc(size2 * sizeof(node));
294+ assert(buf2);
294 memcpy(buf2, b->nodes, b->used_front * sizeof(node));
295 memcpy(buf2, b->nodes, b->used_front * sizeof(node));
295 memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
296 memcpy(&buf2[size2 - b->used_back], &b->nodes[b->size - b->used_back], b->used_back * sizeof(node));
296 free(b->alloc);
297 free(b->alloc);
@@ -305,6 +306,7 @@ void static inline attr_alloc(attr_buffer* b, int ask)
305 if (space >= ask) return;
306 if (space >= ask) return;
306 int size2 = (b->size + 1000 + ask) * 2;
307 int size2 = (b->size + 1000 + ask) * 2;
307 attr* buf2 = malloc(size2 * sizeof(attr));
308 attr* buf2 = malloc(size2 * sizeof(attr));
309+ assert(buf2);
308 memcpy(buf2, b->attrs, b->used * sizeof(attr));
310 memcpy(buf2, b->attrs, b->used * sizeof(attr));
309 free(b->alloc);
311 free(b->alloc);
310 b->size = size2;
312 b->size = size2;
@@ -316,6 +318,7 @@ void set_error(document* d, char* msg)
316{
318{
317 if (d->error_message != NULL) return; // keep the first error message
319 if (d->error_message != NULL) return; // keep the first error message
318 d->error_message = malloc(strlen(msg)+1);
320 d->error_message = malloc(strlen(msg)+1);
321+ assert(d->error_message);
319 strcpy(d->error_message, msg);
322 strcpy(d->error_message, msg);
320}
323}
321
324
@@ -511,6 +514,7 @@ document* document_parse(char* s, int slen)
511 init_parse_table();
514 init_parse_table();
512
515
513 buffer* buf = malloc(sizeof(buffer));
516 buffer* buf = malloc(sizeof(buffer));
517+ assert(buf);
514 document* d = &buf->document;
518 document* d = &buf->document;
515 d->body = s;
519 d->body = s;
516 d->cursor = s;
520 d->cursor = s;
vs/main.c+2
@@ -12,6 +12,7 @@ char* readFile(char* file)
12 int len = ftell(f);
12 int len = ftell(f);
13 fseek(f, 0, SEEK_SET);
13 fseek(f, 0, SEEK_SET);
14 char* res = malloc(len + 1);
14 char* res = malloc(len + 1);
15+ assert(res);
15 res[len] = 0;
16 res[len] = 0;
16 size_t n = fread(res, 1, len, f);
17 size_t n = fread(res, 1, len, f);
17 assert(n == len);
18 assert(n == len);
@@ -31,6 +32,7 @@ void main(int argc, char **argv)
31 {
32 {
32 int len = node_render(doc, document_node(doc), NULL, 0);
33 int len = node_render(doc, document_node(doc), NULL, 0);
33 char* s = malloc(len + 1);
34 char* s = malloc(len + 1);
35+ assert(s);
34 node_render(doc, document_node(doc), s, len);
36 node_render(doc, document_node(doc), s, len);
35 s[len] = 0;
37 s[len] = 0;
36 printf("Parse successful\n"); // , %s\n", s);
38 printf("Parse successful\n"); // , %s\n", s);