diff --git a/lib/xlsxio_read.c b/lib/xlsxio_read.c index 42a9830..e02de1c 100644 --- a/lib/xlsxio_read.c +++ b/lib/xlsxio_read.c @@ -704,7 +704,10 @@ unzGetGlobalInfo(data->zip, &zipglobalinfo); buf[buflen - 1] = 0; while ((status = unzGetCurrentFileInfo(data->zip, NULL, buf, buflen, NULL, 0, NULL, 0)) == UNZ_OK && buf[buflen - 1] != 0) { buflen += UNZIP_FILENAME_BUFFER_STEP; - buf = (char*)realloc(buf, buflen); + if ((buf = (char*)realloc(buf, buflen)) == NULL) { + //memory allocation error + return; + } buf[buflen - 1] = 0; } if (status != UNZ_OK) @@ -1264,12 +1267,10 @@ 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) { - XML_Char *temp = XML_Char_realloc(data->celldata, data->celldatalen + buflen + 1); - if (temp == NULL) { + if ((data->celldata = XML_Char_realloc(data->celldata, data->celldatalen + buflen + 1)) == 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 e59598f..b83de84 100644 --- a/lib/xlsxio_read_sharedstrings.c +++ b/lib/xlsxio_read_sharedstrings.c @@ -195,12 +195,10 @@ 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; - XML_Char *temp = XML_Char_realloc(data->text, data->textlen + buflen); - if (temp == NULL) { + if ((data->text = XML_Char_realloc(data->text, data->textlen + buflen)) == 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 7175aa3..f065855 100644 --- a/lib/xlsxio_write.c +++ b/lib/xlsxio_write.c @@ -158,7 +158,7 @@ const char* docprops_core_xml = const char* docprops_app_xml = XML_HEADER "" OPTIONAL_LINE_BREAK - "" XLSXIOWRITE_NAME " " XLSXIO_VERSION_STRING "" OPTIONAL_LINE_BREAK + "" XLSXIOWRITE_NAME " " XLSXIO_VERSION_STRING "" OPTIONAL_LINE_BREAK "" OPTIONAL_LINE_BREAK; const char* rels_xml = @@ -399,10 +399,10 @@ char* str_replace (char** s, size_t pos, size_t len, char* replacement) if (pos + len > totallen) len = totallen - pos; if (replacementlen > len) { - char *temp = (char*)realloc(*s, totallen - len + replacementlen + 1); - if (temp == NULL) + if ((*s = (char*)realloc(*s, totallen - len + replacementlen + 1)) == NULL) { + //memory allocation error return NULL; - *s = temp; + } } memmove(*s + pos + replacementlen, *s + pos + len, totallen - pos - len + 1); memcpy(*s + pos, replacement, replacementlen); @@ -457,8 +457,10 @@ int vappend_data (char** pdata, size_t* pdatalen, const char* format, va_list ar if ((len = vsnprintf(NULL, 0, format, args)) < 0) return -1; va_end(args); - if ((*pdata = (char*)realloc(*pdata, *pdatalen + len + 1)) == NULL) + if ((*pdata = (char*)realloc(*pdata, *pdatalen + len + 1)) == NULL) { + //memory allocation error return -1; + } vsnprintf(*pdata + *pdatalen, len + 1, format, args2); va_end(args2); *pdatalen += len; @@ -487,10 +489,10 @@ int append_data (char** pdata, size_t* pdatalen, const char* format, ...) va_end(args); if (len < 0) return -1; - char *temp = (char*)realloc(*pdata, *pdatalen + len + 1); - if (temp == NULL) + if ((*pdata = (char*)realloc(*pdata, *pdatalen + len + 1)) == NULL) { + //memory allocation error return -1; - *pdata = temp; + } va_start(args, format); vsnprintf(*pdata + *pdatalen, len + 1, format, args); va_end(args); @@ -510,8 +512,10 @@ int insert_data (char** pdata, size_t* pdatalen, size_t pos, const char* format, va_end(args); if (len < 0) return -1; - if ((*pdata = (char*)realloc(*pdata, *pdatalen + len + 1)) == NULL) + if ((*pdata = (char*)realloc(*pdata, *pdatalen + len + 1)) == NULL) { + //memory allocation error return -1; + } if (pos > *pdatalen) pos = *pdatalen; if (pos < *pdatalen) diff --git a/src/xlsxio_csv2xlsx.c b/src/xlsxio_csv2xlsx.c index 8d05028..82639cd 100644 --- a/src/xlsxio_csv2xlsx.c +++ b/src/xlsxio_csv2xlsx.c @@ -36,10 +36,11 @@ 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 - char *temp = (char*)realloc(*pdata, *pdatalen + bufferdatalen + 1); - if (temp == NULL) + if ((*pdata = (char*)realloc(*pdata, *pdatalen + bufferdatalen + 1)) == NULL) { + //memory allocation error + *pdatalen = 0; return 1; - *pdata = temp; + } //append new data and adjust length memcpy(*pdata + *pdatalen, bufferdata, bufferdatalen); *pdatalen += bufferdatalen;