X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;ds=sidebyside;f=ccan%2Ftal%2Ftest%2Frun-allocfail.c;h=97cba9f6848d37d54594f666292bb82f21dbbce3;hb=61f58ff94e35c9b8ac5488554e2554bc5c9888b3;hp=dd6abff4dc6f7a0678bb3055305330382516143e;hpb=0e34459a02e2615f50bac2767c7dce6632470946;p=ccan diff --git a/ccan/tal/test/run-allocfail.c b/ccan/tal/test/run-allocfail.c index dd6abff4..97cba9f6 100644 --- a/ccan/tal/test/run-allocfail.c +++ b/ccan/tal/test/run-allocfail.c @@ -15,24 +15,33 @@ static void *failing_alloc(size_t len) return malloc(len); } +static void *failing_realloc(void *p, size_t len) +{ + if (alloc_count++ == when_to_fail) + return NULL; + + return realloc(p, len); +} + + static void nofail_on_error(const char *msg) { diag("ERROR: %s", msg); err_count++; } -static void destroy_p(void *p) +static void destroy_p(void *p UNNEEDED) { } int main(void) { - void *p, *c1, *c2; + char *p, *c1, *c2; bool success; - plan_tests(21); + plan_tests(25); - tal_set_backend(failing_alloc, NULL, NULL, nofail_on_error); + tal_set_backend(failing_alloc, failing_realloc, NULL, nofail_on_error); /* Fail at each possible point in an allocation. */ when_to_fail = err_count = 0; @@ -56,6 +65,23 @@ int main(void) ok1(when_to_fail > 1); ok1(err_count == when_to_fail - 1); + /* Now during resize. */ + c2 = c1; + when_to_fail = err_count = 0; + for (;;) { + alloc_count = 0; + if (tal_resize(&c1, 100)) + break; + /* Failing alloc will not change pointer. */ + ok1(c1 == c2); + when_to_fail++; + }; + ok1(alloc_count == 1); + ok1(when_to_fail == 1); + ok1(err_count == 1); + /* Make sure it's really resized. */ + memset(c1, 1, 100); + /* Now for second child. */ when_to_fail = err_count = 0; do { @@ -122,5 +148,6 @@ int main(void) ok1(err_count == when_to_fail - 1); tal_free(p); + tal_cleanup(); return exit_status(); }