From: Vladislav Shchapov Date: Wed, 31 Dec 2025 10:57:08 +0000 (+0500) Subject: Fix warning: 'sprintf' is deprecated X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6bb27e3726010be652d33497995abdb915460db5;p=thirdparty%2Fzlib-ng.git Fix warning: 'sprintf' is deprecated Signed-off-by: Vladislav Shchapov --- diff --git a/test/benchmarks/compressible_data_p.h b/test/benchmarks/compressible_data_p.h index 752a0d6a6..e345bb9c9 100644 --- a/test/benchmarks/compressible_data_p.h +++ b/test/benchmarks/compressible_data_p.h @@ -6,32 +6,49 @@ #ifndef COMPRESSIBLE_DATA_P_H #define COMPRESSIBLE_DATA_P_H + +static inline size_t append_raw(uint8_t *dest, size_t size, const void *src, size_t len) { + if (len > size) len = size; + if (len == 0) return 0; + memcpy(dest, src, len); + return len; +} +static inline size_t append_str(uint8_t *dest, size_t size, const char *src) { + return append_raw(dest, size, src, strlen(src)); +} +static inline size_t append_uint8_t(uint8_t *dest, size_t size, uint8_t src) { + return append_raw(dest, size, &src, 1); +} + // Alloc and initialize buffer with highly compressible data, // interspersed with small amounts of random data and 3-byte matches. -static uint8_t *gen_compressible_data(int bufsize) { +static uint8_t *gen_compressible_data(size_t bufsize) { const char teststr1[42] = "Hello hello World broken Test tast mello."; const char teststr2[32] = "llollollollollo He Te me orld"; const char teststr3[4] = "bro"; int loops = 0; - uint8_t *buffer = (uint8_t *)malloc(bufsize + 96); // Need extra space for init loop overrun + uint8_t *buffer = (uint8_t *)malloc(bufsize); if (buffer == NULL) { return NULL; } - for (int pos = 0; pos < bufsize; ){ - pos += sprintf((char *)buffer+pos, "%s", teststr1); - buffer[pos++] = (uint8_t)(rand() & 0xFF); + for (size_t pos = 0; pos < bufsize; ) { + pos += append_str(buffer+pos, bufsize-pos, teststr1); + pos += append_uint8_t(buffer+pos, bufsize-pos, (uint8_t)(rand() & 0xFF)); // Every so often, add a few other little bits to break the pattern if (loops % 13 == 0) { - pos += sprintf((char *)buffer+pos, "%s", teststr3); - buffer[pos++] = (uint8_t)(rand() & 0xFF); + pos += append_str(buffer+pos, bufsize-pos, teststr3); + pos += append_uint8_t(buffer+pos, bufsize-pos, (uint8_t)(rand() & 0xFF)); } if (loops % 300 == 0) { // Only found once or twice per window - pos += sprintf((char *)buffer+pos, "%s", teststr2); + pos += append_str(buffer+pos, bufsize-pos, teststr2); } loops++; } + if (bufsize > 0) { + buffer[bufsize-1] = 0; + } return buffer; } #endif