From 4518ae5aa3e59f45e58202e5b877ec1d4e05f279 Mon Sep 17 00:00:00 2001 From: Syping Date: Wed, 5 Nov 2025 11:58:45 +0100 Subject: [PATCH] libragephoto: impl. support for NULL/nullptr to clear data values --- src/core/RagePhoto.c | 103 +++++++++++++++++++++------------------- src/core/RagePhoto.cpp | 104 ++++++++++++++++++++++------------------- 2 files changed, 112 insertions(+), 95 deletions(-) diff --git a/src/core/RagePhoto.c b/src/core/RagePhoto.c index edcc8e4..38e4b56 100644 --- a/src/core/RagePhoto.c +++ b/src/core/RagePhoto.c @@ -124,37 +124,40 @@ static inline size_t zeroBuffer(char *output, size_t *pos, size_t outputLen, siz static inline bool writeDataChar(const char *input, char **output) { - const size_t src_s = strlen(input) + 1; - if (*output) { - const size_t dst_s = strlen(*output) + 1; - if (dst_s > src_s) { - char *t_output = (char*)realloc(*output, src_s); - if (!t_output) { - return false; + if (input) { + const size_t src_s = strlen(input) + 1; + if (*output) { + const size_t dst_s = strlen(*output) + 1; + if (dst_s > src_s) { + char *t_output = (char*)realloc(*output, src_s); + if (!t_output) + return false; + *output = t_output; + memcpy(*output, input, src_s); } - *output = t_output; - memcpy(*output, input, src_s); - } - else if (dst_s < src_s) { - char *t_output = (char*)malloc(src_s); - if (!t_output) { - return false; + else if (dst_s < src_s) { + char *t_output = (char*)malloc(src_s); + if (!t_output) + return false; + free(*output); + *output = t_output; + memcpy(*output, input, src_s); + } + else { + memcpy(*output, input, src_s); } - free(*output); - *output = t_output; - memcpy(*output, input, src_s); } else { + char *t_output = (char*)malloc(src_s); + if (!t_output) + return false; + *output = t_output; memcpy(*output, input, src_s); } } - else { - char *t_output = (char*)malloc(src_s); - if (!t_output) { - return false; - } - *output = t_output; - memcpy(*output, input, src_s); + else if (*output) { + free(*output); + *output = NULL; } return true; } @@ -1024,7 +1027,7 @@ bool ragephoto_savefilef(ragephoto_t instance, const char *filename, uint32_t ph const size_t writeSize = fwrite(data, sizeof(char), fileSize, file); fclose(file); free(data); - return (fileSize == writeSize); + return fileSize == writeSize; } bool ragephoto_savefile(ragephoto_t instance, const char *filename) @@ -1192,19 +1195,33 @@ void ragephoto_setphotoformat(ragephoto_t instance, uint32_t photoFormat) bool ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t size, uint32_t bufferSize) { - if (instance->data->jpeg) { - if (instance->data->jpegSize > size) { - char *t_photoData = (char*)realloc(instance->data->jpeg, size); - if (!t_photoData) { - instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16 - return false; + if (data && size) { + if (instance->data->jpeg) { + if (instance->data->jpegSize > size) { + char *t_photoData = (char*)realloc(instance->data->jpeg, size); + if (!t_photoData) { + instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16 + return false; + } + instance->data->jpeg = t_photoData; + memcpy(instance->data->jpeg, data, size); + instance->data->jpegSize = size; + } + else if (instance->data->jpegSize < size) { + free(instance->data->jpeg); + instance->data->jpeg = (char*)malloc(size); + if (!instance->data->jpeg) { + instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16 + return false; + } + memcpy(instance->data->jpeg, data, size); + instance->data->jpegSize = size; + } + else { + memcpy(instance->data->jpeg, data, size); } - instance->data->jpeg = t_photoData; - memcpy(instance->data->jpeg, data, size); - instance->data->jpegSize = size; } - else if (instance->data->jpegSize < size) { - free(instance->data->jpeg); + else { instance->data->jpeg = (char*)malloc(size); if (!instance->data->jpeg) { instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16 @@ -1213,18 +1230,10 @@ bool ragephoto_setphotojpeg(ragephoto_t instance, const char *data, uint32_t siz memcpy(instance->data->jpeg, data, size); instance->data->jpegSize = size; } - else { - memcpy(instance->data->jpeg, data, size); - } } - else { - instance->data->jpeg = (char*)malloc(size); - if (!instance->data->jpeg) { - instance->data->error = RAGEPHOTO_ERROR_PHOTOMALLOCERROR; // 16 - return false; - } - memcpy(instance->data->jpeg, data, size); - instance->data->jpegSize = size; + else if (instance->data->jpeg) { + free(instance->data->jpeg); + instance->data->jpeg = NULL; } if (bufferSize != 0) { diff --git a/src/core/RagePhoto.cpp b/src/core/RagePhoto.cpp index 2628d29..429e1d3 100644 --- a/src/core/RagePhoto.cpp +++ b/src/core/RagePhoto.cpp @@ -118,37 +118,40 @@ inline size_t zeroBuffer(char *output, size_t *pos, size_t outputLen, size_t inp inline bool writeDataChar(const char *input, char **output) { - const size_t src_s = strlen(input) + 1; - if (*output) { - const size_t dst_s = strlen(*output) + 1; - if (dst_s > src_s) { - char *t_output = static_cast(realloc(*output, src_s)); - if (!t_output) { - return false; + if (input) { + const size_t src_s = strlen(input) + 1; + if (*output) { + const size_t dst_s = strlen(*output) + 1; + if (dst_s > src_s) { + char *t_output = static_cast(realloc(*output, src_s)); + if (!t_output) + return false; + *output = t_output; + memcpy(*output, input, src_s); } - *output = t_output; - memcpy(*output, input, src_s); - } - else if (dst_s < src_s) { - char *t_output = static_cast(malloc(src_s)); - if (!t_output) { - return false; + else if (dst_s < src_s) { + char *t_output = static_cast(malloc(src_s)); + if (!t_output) + return false; + free(*output); + *output = t_output; + memcpy(*output, input, src_s); + } + else { + memcpy(*output, input, src_s); } - free(*output); - *output = t_output; - memcpy(*output, input, src_s); } else { + char *t_output = static_cast(malloc(src_s)); + if (!t_output) + return false; + *output = t_output; memcpy(*output, input, src_s); } } - else { - char *t_output = static_cast(malloc(src_s)); - if (!t_output) { - return false; - } - *output = t_output; - memcpy(*output, input, src_s); + else if (*output) { + free(*output); + *output = nullptr; } return true; } @@ -1018,8 +1021,7 @@ bool RagePhoto::saveFile(const char *filename, uint32_t photoFormat) ofs.close(); return ok; } - else - return false; + return false; } bool RagePhoto::saveFile(const char *filename) @@ -1189,19 +1191,33 @@ void RagePhoto::setHeader(const char *header, uint32_t headerSum, uint32_t heade bool RagePhoto::setJpeg(const char *data, uint32_t size, uint32_t bufferSize) { - if (m_data->jpeg) { - if (m_data->jpegSize > size) { - char *t_photoData = static_cast(realloc(m_data->jpeg, size)); - if (!t_photoData) { - m_data->error = Error::PhotoMallocError; // 16 - return false; + if (data && size) { + if (m_data->jpeg) { + if (m_data->jpegSize > size) { + char *t_photoData = static_cast(realloc(m_data->jpeg, size)); + if (!t_photoData) { + m_data->error = Error::PhotoMallocError; // 16 + return false; + } + m_data->jpeg = t_photoData; + memcpy(m_data->jpeg, data, size); + m_data->jpegSize = size; + } + else if (m_data->jpegSize < size) { + free(m_data->jpeg); + m_data->jpeg = static_cast(malloc(size)); + if (!m_data->jpeg) { + m_data->error = Error::PhotoMallocError; // 16 + return false; + } + memcpy(m_data->jpeg, data, size); + m_data->jpegSize = size; + } + else { + memcpy(m_data->jpeg, data, size); } - m_data->jpeg = t_photoData; - memcpy(m_data->jpeg, data, size); - m_data->jpegSize = size; } - else if (m_data->jpegSize < size) { - free(m_data->jpeg); + else { m_data->jpeg = static_cast(malloc(size)); if (!m_data->jpeg) { m_data->error = Error::PhotoMallocError; // 16 @@ -1210,18 +1226,10 @@ bool RagePhoto::setJpeg(const char *data, uint32_t size, uint32_t bufferSize) memcpy(m_data->jpeg, data, size); m_data->jpegSize = size; } - else { - memcpy(m_data->jpeg, data, size); - } } - else { - m_data->jpeg = static_cast(malloc(size)); - if (!m_data->jpeg) { - m_data->error = Error::PhotoMallocError; // 16 - return false; - } - memcpy(m_data->jpeg, data, size); - m_data->jpegSize = size; + else if (m_data->jpeg) { + free(m_data->jpeg); + m_data->jpeg = nullptr; } if (bufferSize != 0) {