1
0

catch-alloc-failures.patch 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. Description: Catch *alloc failures
  2. Author: Christoph Biedl <debian.axhn@manchmal.in-ulm.de>
  3. Bug: https://bitbucket.org/ripencc/bgpdump/issues/28/
  4. Last-Update: 2016-07-13
  5. Better exit(1) than doing null pointer accesses
  6. --- a/bgpdump_lib.c
  7. +++ b/bgpdump_lib.c
  8. @@ -94,7 +94,7 @@
  9. return NULL;
  10. }
  11. - BGPDUMP *this_dump = malloc(sizeof(BGPDUMP));
  12. + BGPDUMP *this_dump = malloc_check(sizeof(BGPDUMP));
  13. strcpy(this_dump->filename, "[STDIN]");
  14. if(filename && strcmp(filename, "-")) {
  15. if (strlen(filename) >= BGPDUMP_MAX_FILE_LEN - 1) {
  16. @@ -137,7 +137,7 @@
  17. int ok=0;
  18. u_int32_t bytes_read;
  19. - this_entry = malloc(sizeof(BGPDUMP_ENTRY));
  20. + this_entry = malloc_check(sizeof(BGPDUMP_ENTRY));
  21. bytes_read = cfr_read_n(dump->f, &(this_entry->time), 4);
  22. bytes_read += cfr_read_n(dump->f, &(this_entry->type), 2);
  23. @@ -182,7 +182,7 @@
  24. dump->parsed++;
  25. this_entry->attr=NULL;
  26. - buffer = malloc(this_entry->length);
  27. + buffer = malloc_check(this_entry->length);
  28. bytes_read = cfr_read_n(dump->f, buffer, this_entry->length);
  29. if(bytes_read != this_entry->length) {
  30. err("bgpdump_read_next: incomplete dump record (%d bytes read, expecting %d)",
  31. @@ -456,7 +456,7 @@
  32. free(table_dump_v2_peer_index_table);
  33. }
  34. - table_dump_v2_peer_index_table = malloc(sizeof(BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE));
  35. + table_dump_v2_peer_index_table = malloc_check(sizeof(BGPDUMP_TABLE_DUMP_V2_PEER_INDEX_TABLE));
  36. t = table_dump_v2_peer_index_table;
  37. t->entries = NULL;
  38. @@ -793,7 +793,7 @@
  39. entry->body.zebra_message.notify_len = entry->body.zebra_message.size - 21;
  40. if(entry->body.zebra_message.notify_len > 0) {
  41. - entry->body.zebra_message.notify_data = malloc(entry->body.zebra_message.notify_len);
  42. + entry->body.zebra_message.notify_data = malloc_check(entry->body.zebra_message.notify_len);
  43. mstream_get(s, entry->body.zebra_message.notify_data, entry->body.zebra_message.notify_len);
  44. }
  45. @@ -808,7 +808,7 @@
  46. mstream_getc(s, &entry->body.zebra_message.opt_len);
  47. if(entry->body.zebra_message.opt_len) {
  48. - entry->body.zebra_message.opt_data = malloc(entry->body.zebra_message.opt_len);
  49. + entry->body.zebra_message.opt_data = malloc_check(entry->body.zebra_message.opt_len);
  50. mstream_get(s, entry->body.zebra_message.opt_data, entry->body.zebra_message.opt_len);
  51. }
  52. @@ -844,9 +844,9 @@
  53. static attributes_t *attr_init(struct mstream *s, int len) {
  54. - attributes_t *attr = malloc(sizeof(struct attr));
  55. + attributes_t *attr = malloc_check(sizeof(struct attr));
  56. - attr->data=malloc(len);
  57. + attr->data=malloc_check(len);
  58. memcpy(attr->data, &s->start[s->position], len);
  59. attr->len = len;
  60. @@ -865,7 +865,7 @@
  61. attr->aspath = NULL;
  62. attr->community = NULL;
  63. attr->transit = NULL;
  64. - attr->mp_info = calloc(1, sizeof(struct mp_info));;
  65. + attr->mp_info = calloc_check(1, sizeof(struct mp_info));;
  66. attr->unknown_num = 0;
  67. attr->unknown = NULL;
  68. @@ -881,14 +881,14 @@
  69. static void process_unknown_attr(struct mstream *s, attributes_t *attr, int flag, int type, int len) {
  70. /* Unknown attribute. Save as is */
  71. attr->unknown_num++;
  72. - attr->unknown = realloc(attr->unknown, attr->unknown_num * sizeof(struct unknown_attr));
  73. + attr->unknown = realloc_check(attr->unknown, attr->unknown_num * sizeof(struct unknown_attr));
  74. /* Pointer to the unknown attribute we want to fill in */
  75. struct unknown_attr unknown = {
  76. .flag = flag,
  77. .type = type,
  78. .len = len,
  79. - .raw = malloc(len)
  80. + .raw = malloc_check(len)
  81. };
  82. attr->unknown[attr->unknown_num - 1] = unknown;
  83. @@ -956,9 +956,9 @@
  84. break;
  85. case BGP_ATTR_COMMUNITIES:
  86. assert(! attr->community);
  87. - attr->community = malloc(sizeof(struct community));
  88. + attr->community = malloc_check(sizeof(struct community));
  89. attr->community->size = len / 4;
  90. - attr->community->val = malloc(len);
  91. + attr->community->val = malloc_check(len);
  92. mstream_get(s,attr->community->val,len);
  93. attr->community->str = NULL;
  94. process_attr_community_string(attr->community);
  95. @@ -982,9 +982,9 @@
  96. break;
  97. case BGP_ATTR_CLUSTER_LIST:
  98. assert(! attr->cluster);
  99. - attr->cluster = malloc(sizeof(struct cluster_list));
  100. + attr->cluster = malloc_check(sizeof(struct cluster_list));
  101. attr->cluster->length = len/4;
  102. - attr->cluster->list = malloc((attr->cluster->length) * sizeof(struct in_addr));
  103. + attr->cluster->list = malloc_check((attr->cluster->length) * sizeof(struct in_addr));
  104. int i; // cluster index
  105. for (i = 0; i < attr->cluster->length; i++)
  106. @@ -1014,14 +1014,14 @@
  107. }
  108. struct aspath *create_aspath(u_int16_t len, u_int8_t asn_len) {
  109. - struct aspath *aspath = malloc(sizeof(struct aspath));
  110. + struct aspath *aspath = malloc_check(sizeof(struct aspath));
  111. if(aspath) {
  112. aspath->asn_len = asn_len;
  113. aspath->length = len;
  114. aspath->count = 0;
  115. aspath->str = NULL;
  116. if(len > 0)
  117. - aspath->data = malloc(len);
  118. + aspath->data = malloc_check(len);
  119. else
  120. aspath->data = NULL;
  121. }
  122. @@ -1036,13 +1036,13 @@
  123. as->str = NULL;
  124. }
  125. - as->str = malloc(strlen(ASPATH_STR_ERROR) + 1);
  126. + as->str = malloc_check(strlen(ASPATH_STR_ERROR) + 1);
  127. strcpy(as->str, ASPATH_STR_ERROR);
  128. }
  129. void process_attr_aspath_string(struct aspath *as) {
  130. const int MAX_ASPATH_LEN = 8000;
  131. - as->str = malloc(MAX_ASPATH_LEN);
  132. + as->str = malloc_check(MAX_ASPATH_LEN);
  133. /* Set default values */
  134. int space = 0;
  135. @@ -1210,12 +1210,12 @@
  136. }
  137. }
  138. - com->str = malloc(strlen(buf)+1);
  139. + com->str = malloc_check(strlen(buf)+1);
  140. strcpy(com->str, buf);
  141. }
  142. static struct mp_nlri *get_nexthop(struct mstream *s, u_int16_t afi) {
  143. - struct mp_nlri *nlri = calloc(1, sizeof(struct mp_nlri));
  144. + struct mp_nlri *nlri = calloc_check(1, sizeof(struct mp_nlri));
  145. nlri->nexthop_len = mstream_getc(s, NULL);
  146. @@ -1300,7 +1300,7 @@
  147. }
  148. /* Allocate structure */
  149. - mp_nlri = malloc(sizeof(struct mp_nlri));
  150. + mp_nlri = malloc_check(sizeof(struct mp_nlri));
  151. memset(mp_nlri, 0, sizeof(struct mp_nlri));
  152. info->withdraw[afi][safi] = mp_nlri;
  153. @@ -1439,7 +1439,7 @@
  154. while(mergedpath->count < path->count - newpath->count) {
  155. /* Make room */
  156. newlen = mergedpath->length + sizeof(struct assegment) + segment->length * ASN32_LEN;
  157. - mergedpath->data = realloc(mergedpath->data, newlen);
  158. + mergedpath->data = realloc_check(mergedpath->data, newlen);
  159. /* Create a new AS-path segment */
  160. mergedsegment = (struct assegment *) (mergedpath->data + mergedpath->length);
  161. @@ -1464,7 +1464,7 @@
  162. }
  163. /* Append NEW_AS_PATH to merged path */
  164. - mergedpath->data = realloc(mergedpath->data, mergedpath->length + newpath->length);
  165. + mergedpath->data = realloc_check(mergedpath->data, mergedpath->length + newpath->length);
  166. memcpy(mergedpath->data + mergedpath->length, newpath->data, newpath->length);
  167. mergedpath->length += newpath->length;
  168. --- a/cfile_tools.c
  169. +++ b/cfile_tools.c
  170. @@ -73,7 +73,7 @@
  171. format = 2; // skip specials 0, 1
  172. // Do action dependent on file format
  173. - retval = (CFRFILE *) calloc(1,sizeof(CFRFILE));
  174. + retval = (CFRFILE *) calloc_check(1,sizeof(CFRFILE));
  175. retval->eof = 0;
  176. retval->error1 = 0;
  177. retval->error2 = 0;
  178. @@ -357,12 +357,12 @@
  179. // allocate initial buffer if none was passed or size was zero
  180. if (*lineptr == NULL) {
  181. - *lineptr = (char *) calloc(120, 1);
  182. + *lineptr = (char *) calloc_check(120, 1);
  183. *n = 120;
  184. }
  185. if (*n == 0) {
  186. *n = 120;
  187. - *lineptr = (char *) realloc(*lineptr, *n); // to avoid memory-leaks
  188. + *lineptr = (char *) realloc_check(*lineptr, *n); // to avoid memory-leaks
  189. }
  190. count = 0;
  191. @@ -375,7 +375,7 @@
  192. count ++;
  193. if (count >= *n) {
  194. *n = 2 * *n;
  195. - *lineptr = (char *) realloc(*lineptr, *n);
  196. + *lineptr = (char *) realloc_check(*lineptr, *n);
  197. if (*lineptr == NULL) {
  198. stream->error1 = errno;
  199. return(-1);
  200. @@ -551,3 +551,29 @@
  201. }
  202. #endif
  203. +void *malloc_check(size_t __size) {
  204. + void *ptr = malloc(__size);
  205. + if(ptr==NULL) {
  206. + fputs ("Out of memory!", stderr);
  207. + exit(1);
  208. + }
  209. + return ptr;
  210. +}
  211. +
  212. +void *calloc_check(size_t __nmemb, size_t __size) {
  213. + void *ptr = calloc(__nmemb, __size);
  214. + if(ptr==NULL) {
  215. + fputs ("Out of memory!", stderr);
  216. + exit(1);
  217. + }
  218. + return ptr;
  219. +}
  220. +
  221. +void *realloc_check(void *__ptr, size_t __size) {
  222. + void *ptr = realloc(__ptr, __size);
  223. + if(ptr==NULL) {
  224. + fputs ("Out of memory!", stderr);
  225. + exit(1);
  226. + }
  227. + return ptr;
  228. +}
  229. --- a/cfile_tools.h
  230. +++ b/cfile_tools.h
  231. @@ -89,5 +89,8 @@
  232. const char * _bz2_strerror(int err);
  233. #endif
  234. +void *malloc_check(size_t size);
  235. +void *calloc_check(size_t __nmemb, size_t __size);
  236. +void *realloc_check(void *__ptr, size_t __size);
  237. #endif