From 71e60437194f32efd151a9241dc91bad27b80d8a Mon Sep 17 00:00:00 2001 From: ckolivas Date: Mon, 19 Jan 2015 15:11:15 +1100 Subject: [PATCH] Perform back off and retry on failed realloc in jansson strbuffer_append_bytes instead of failing --- src/jansson-2.6/src/strbuffer.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/jansson-2.6/src/strbuffer.c b/src/jansson-2.6/src/strbuffer.c index c8abf46e..b93231cd 100644 --- a/src/jansson-2.6/src/strbuffer.c +++ b/src/jansson-2.6/src/strbuffer.c @@ -11,6 +11,7 @@ #include #include +#include #include "jansson_private.h" #include "strbuffer.h" @@ -74,6 +75,7 @@ int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size) { if(size >= strbuff->size - strbuff->length) { + int backoff = 1; size_t new_size; char *new_value; @@ -86,9 +88,13 @@ int strbuffer_append_bytes(strbuffer_t *strbuff, const char *data, size_t size) new_size = max(strbuff->size * STRBUFFER_FACTOR, strbuff->length + size + 1); - new_value = realloc(strbuff->value, new_size); - if(!new_value) - return -1; + while (42) { + new_value = realloc(strbuff->value, new_size); + if (new_value) + break; + usleep(backoff * 1000); + backoff <<= 1; + } strbuff->value = new_value; strbuff->size = new_size;