Skip to content

Commit

Permalink
Adjust realloc changes
Browse files Browse the repository at this point in the history
  • Loading branch information
brechtsanders committed Jan 7, 2025
1 parent cc4c014 commit c5df469
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
9 changes: 5 additions & 4 deletions lib/xlsxio_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions lib/xlsxio_read_sharedstrings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 13 additions & 9 deletions lib/xlsxio_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const char* docprops_core_xml =
const char* docprops_app_xml =
XML_HEADER
"<Properties xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\" xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\">" OPTIONAL_LINE_BREAK
"<Application>" XLSXIOWRITE_NAME " " XLSXIO_VERSION_STRING "</Application>" OPTIONAL_LINE_BREAK
"<Application>" XLSXIOWRITE_NAME " " XLSXIO_VERSION_STRING "</Application>" OPTIONAL_LINE_BREAK
"</Properties>" OPTIONAL_LINE_BREAK;

const char* rels_xml =
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions src/xlsxio_csv2xlsx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c5df469

Please sign in to comment.