small fix for ccan/take/take.c
authorDmitry Petukhov <dp@simplexum.com>
Sun, 4 Nov 2018 14:58:04 +0000 (19:58 +0500)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 5 Nov 2018 02:44:52 +0000 (13:14 +1030)
Hello.

I've decided to take a look at c-lighting code, because we might
consider using it in the future.

I found a small problem in ccan/take/take.c that only concerns code
that runs with CCAN_TAKE_DEBUG set (DEVELOPER=1 for c-lighting).

It is a small issue, but I decided to notify you as the author of the
code, anyway.

the issue is:
  in take_() function, potential failure of realloc for labelarr is not
  handled.

I attached a diff with a fix.

I thought that making a pull request for c-lighting would not be right,
as ccan is a separate project, but I did not find a way to report this
at http://git.ozlabs.org/, where ccan repo resides.

Therefore I wrote to you directly.

[ Minor whitespace changes --RR ]

ccan/take/take.c

index c628aac0dc97ff663db216e4f05a14b8cfd38bbf..4833bf93576473a964e0cb6eab120af7f49c1fd5 100644 (file)
@@ -32,9 +32,20 @@ void *take_(const void *p, const char *label)
                }
                takenarr = new;
                /* Once labelarr is set, we maintain it. */
-               if (labelarr)
-                       labelarr = realloc(labelarr,
-                                          sizeof(*labelarr) * (max_taken+1));
+               if (labelarr) {
+                        const char **labelarr_new;
+                       labelarr_new = realloc(labelarr,
+                                              sizeof(*labelarr) * (max_taken+1));
+                        if (labelarr_new) {
+                                labelarr = labelarr_new;
+                        } else {
+                                /* num_taken will be out of sync with the size of
+                                 * labelarr after realloc failure.
+                                 * Just pretend that we never had labelarr allocated. */
+                                free(labelarr);
+                                labelarr = NULL;
+                        }
+                }
                max_taken++;
        }
        if (unlikely(labelarr))