]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
examples : sprintf -> snprintf (#8434)
authorGeorgi Gerganov <redacted>
Fri, 12 Jul 2024 07:46:14 +0000 (10:46 +0300)
committerGitHub <redacted>
Fri, 12 Jul 2024 07:46:14 +0000 (10:46 +0300)
* examples : sprintf -> snprintf

ggml-ci

* examples : use sizeof() instead of hardcoded constants

examples/eval-callback/eval-callback.cpp
examples/gguf-hash/gguf-hash.cpp

index 64cd338c2635134099ef55f8174e1289930c31f2..c8a3016a4d80acd85646c7a7a3b10f1ce9676098 100644 (file)
@@ -99,7 +99,7 @@ static bool ggml_debug(struct ggml_tensor * t, bool ask, void * user_data) {
 
     char src1_str[128] = {0};
     if (src1) {
-        sprintf(src1_str, "%s{%s}", src1->name, ggml_ne_string(src1).c_str());
+        snprintf(src1_str, sizeof(src1_str), "%s{%s}", src1->name, ggml_ne_string(src1).c_str());
     }
 
     printf("%s: %24s = (%s) %10s(%s{%s}, %s}) = {%s}\n", __func__,
index c34728c3df4bea3c214e7045cbd3bb7928247b45..e96c75117f53350a0f1e545a39aebb2c5c004fa6 100644 (file)
@@ -347,7 +347,7 @@ static hash_exit_code_t gguf_hash(const hash_params & hash_params) {
                 char hex_result[17];
                 for (int  offset = 0; offset < 8; offset++) {
                     unsigned int shift_bits_by = (8 * (8 - offset - 1));
-                    sprintf( ( hex_result + (2*offset)), "%02x", (unsigned char) (hash >> shift_bits_by)&0xff);
+                    snprintf( ( hex_result + (2*offset)), sizeof(hex_result) - (2*offset), "%02x", (unsigned char) (hash >> shift_bits_by)&0xff);
                 }
 
                 if (hash_params.manifest_is_usable) {
@@ -384,7 +384,7 @@ static hash_exit_code_t gguf_hash(const hash_params & hash_params) {
 
                 char hex_result[41] = {0};
                 for (int  offset = 0; offset < 20; offset++) {
-                    sprintf( ( hex_result + (2*offset)), "%02x", result[offset]&0xff);
+                    snprintf( ( hex_result + (2*offset)), sizeof(hex_result) - (2*offset), "%02x", result[offset]&0xff);
                 }
 
                 if (hash_params.manifest_is_usable) {
@@ -421,7 +421,7 @@ static hash_exit_code_t gguf_hash(const hash_params & hash_params) {
 
                 char hex_result[SHA256_DIGEST_SIZE * 2 + 1] = {0};
                 for (int  offset = 0; offset < SHA256_DIGEST_SIZE; offset++) {
-                    sprintf( ( hex_result + (2*offset)), "%02x", result[offset]&0xff);
+                    snprintf( ( hex_result + (2*offset)), sizeof(hex_result) - (2*offset), "%02x", result[offset]&0xff);
                 }
 
                 if (hash_params.manifest_is_usable) {
@@ -460,7 +460,7 @@ static hash_exit_code_t gguf_hash(const hash_params & hash_params) {
         char hex_result[17];
         for (int  offset = 0; offset < 8; offset++) {
             unsigned int shift_bits_by = (8 * (8 - offset - 1));
-            sprintf( ( hex_result + (2*offset)), "%02x", (unsigned char) (hash >> shift_bits_by)&0xff);
+            snprintf( ( hex_result + (2*offset)), sizeof(hex_result) - (2*offset), "%02x", (unsigned char) (hash >> shift_bits_by)&0xff);
         }
 
         if (hash_params.manifest_is_usable) {
@@ -490,7 +490,7 @@ static hash_exit_code_t gguf_hash(const hash_params & hash_params) {
 
         char hex_result[41];
         for (int  offset = 0; offset < 20; offset++) {
-            sprintf( ( hex_result + (2*offset)), "%02x", result[offset]&0xff);
+            snprintf( ( hex_result + (2*offset)), sizeof(hex_result) - (2*offset), "%02x", result[offset]&0xff);
         }
 
         if (hash_params.manifest_is_usable) {
@@ -520,7 +520,7 @@ static hash_exit_code_t gguf_hash(const hash_params & hash_params) {
 
         char hex_result[SHA256_DIGEST_SIZE * 2 + 1] = {0};
         for (int  offset = 0; offset < SHA256_DIGEST_SIZE; offset++) {
-            sprintf( ( hex_result + (2*offset)), "%02x", result[offset]&0xff);
+            snprintf( ( hex_result + (2*offset)), sizeof(hex_result) - (2*offset), "%02x", result[offset]&0xff);
         }
 
         if (hash_params.manifest_is_usable) {
@@ -552,7 +552,7 @@ static hash_exit_code_t gguf_hash(const hash_params & hash_params) {
         generate_uuidv5(result, uuid);
 
         char string_buffer[37] = {0};
-        sprintf(string_buffer, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
+        snprintf(string_buffer, sizeof(string_buffer), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
             uuid[0], uuid[1], uuid[2], uuid[3],
             uuid[4], uuid[5], uuid[6], uuid[7],
             uuid[8], uuid[9], uuid[10], uuid[11],