]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Fix warning: 'sprintf' is deprecated
authorVladislav Shchapov <vladislav@shchapov.ru>
Wed, 31 Dec 2025 10:57:08 +0000 (15:57 +0500)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Wed, 7 Jan 2026 06:46:48 +0000 (07:46 +0100)
Signed-off-by: Vladislav Shchapov <vladislav@shchapov.ru>
test/benchmarks/compressible_data_p.h

index 752a0d6a6976f6868bec8ad014aa91d00726614f..e345bb9c9185b90e05cd0d28c274efe7820add86 100644 (file)
@@ -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