diff --git a/lib/xlsxio_read.c b/lib/xlsxio_read.c index b1ccd26..42a9830 100644 --- a/lib/xlsxio_read.c +++ b/lib/xlsxio_read.c @@ -1264,10 +1264,12 @@ void data_sheet_expat_callback_value_data (void* callbackdata, const XML_Char* b { struct data_sheet_callback_data* data = (struct data_sheet_callback_data*)callbackdata; if (data->cell_string_type != none) { - if ((data->celldata = XML_Char_realloc(data->celldata, data->celldatalen + buflen + 1)) == NULL) { + XML_Char *temp = XML_Char_realloc(data->celldata, data->celldatalen + buflen + 1); + if (temp == NULL) { //memory allocation error data->celldatalen = 0; } else { + data->celldata = temp; //add new data to value buffer XML_Char_poscpy(data->celldata, data->celldatalen, buf, buflen); data->celldatalen += buflen; diff --git a/lib/xlsxio_read_sharedstrings.c b/lib/xlsxio_read_sharedstrings.c index b83de84..e59598f 100644 --- a/lib/xlsxio_read_sharedstrings.c +++ b/lib/xlsxio_read_sharedstrings.c @@ -195,10 +195,12 @@ void shared_strings_callback_find_shared_string_end (void* callbackdata, const X void shared_strings_callback_string_data (void* callbackdata, const XML_Char* buf, int buflen) { struct shared_strings_callback_data* data = (struct shared_strings_callback_data*)callbackdata; - if ((data->text = XML_Char_realloc(data->text, data->textlen + buflen)) == NULL) { + XML_Char *temp = XML_Char_realloc(data->text, data->textlen + buflen); + if (temp == NULL) { //memory allocation error data->textlen = 0; } else { + data->text = temp; XML_Char_poscpy(data->text, data->textlen, buf, buflen); data->textlen += buflen; } diff --git a/lib/xlsxio_write.c b/lib/xlsxio_write.c index 178873f..7175aa3 100644 --- a/lib/xlsxio_write.c +++ b/lib/xlsxio_write.c @@ -398,9 +398,12 @@ char* str_replace (char** s, size_t pos, size_t len, char* replacement) pos = totallen; if (pos + len > totallen) len = totallen - pos; - if (replacementlen > len) - if ((*s = (char*)realloc(*s, totallen - len + replacementlen + 1)) == NULL) + if (replacementlen > len) { + char *temp = (char*)realloc(*s, totallen - len + replacementlen + 1); + if (temp == NULL) return NULL; + *s = temp; + } memmove(*s + pos + replacementlen, *s + pos + len, totallen - pos - len + 1); memcpy(*s + pos, replacement, replacementlen); return *s; @@ -484,8 +487,10 @@ int append_data (char** pdata, size_t* pdatalen, const char* format, ...) va_end(args); if (len < 0) return -1; - if ((*pdata = (char*)realloc(*pdata, *pdatalen + len + 1)) == NULL) + char *temp = (char*)realloc(*pdata, *pdatalen + len + 1); + if (temp == NULL) return -1; + *pdata = temp; va_start(args, format); vsnprintf(*pdata + *pdatalen, len + 1, format, args); va_end(args); diff --git a/src/xlsxio_csv2xlsx.c b/src/xlsxio_csv2xlsx.c index ecc5483..8d05028 100644 --- a/src/xlsxio_csv2xlsx.c +++ b/src/xlsxio_csv2xlsx.c @@ -36,8 +36,10 @@ THE SOFTWARE. int append_buffer_data (char** pdata, size_t* pdatalen, const char* bufferdata, size_t bufferdatalen) { //allocate larger data buffer, abort in case of memory allocation error - if ((*pdata = (char*)realloc(*pdata, *pdatalen + bufferdatalen + 1)) == NULL) + char *temp = (char*)realloc(*pdata, *pdatalen + bufferdatalen + 1); + if (temp == NULL) return 1; + *pdata = temp; //append new data and adjust length memcpy(*pdata + *pdatalen, bufferdata, bufferdatalen); *pdatalen += bufferdatalen;