@@ -6625,36 +6625,35 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
66256625                            case  GGUF_TYPE_FLOAT64 :
66266626                            case  GGUF_TYPE_BOOL :
66276627                                {
6628-                                     // prevent integer overflow in the malloc  below 
6628+                                     // prevent integer overflow in the calloc  below 
66296629                                    if  (kv -> value .arr .n  >= SIZE_MAX /gguf_type_size (kv -> value .arr .type )) {
66306630                                        fprintf (stderr , "%s: array size is too large (%"  PRIu64  ")\n" , __func__ , kv -> value .arr .n );
66316631                                        fclose (file );
66326632                                        gguf_free (ctx );
66336633                                        return  NULL ;
66346634                                    }
66356635
6636-                                     const  size_t  nbytes  =  kv -> value .arr .n  *  gguf_type_size (kv -> value .arr .type );
6637-                                     kv -> value .arr .data  =  malloc (nbytes );
6636+                                     kv -> value .arr .data  =  calloc (kv -> value .arr .n , gguf_type_size (kv -> value .arr .type ));
66386637                                    if  (!kv -> value .arr .data ) {
66396638                                        fprintf (stderr , "%s: failed to allocate memory for array\n" , __func__ );
66406639                                        fclose (file );
66416640                                        gguf_free (ctx );
66426641                                        return  NULL ;
66436642                                    }
66446643
6645-                                     ok  =  ok  &&  gguf_fread_el (file , kv -> value .arr .data , nbytes , & offset );
6644+                                     ok  =  ok  &&  gguf_fread_el (file , kv -> value .arr .data , kv -> value . arr . n   *   gguf_type_size ( kv -> value . arr . type ) , & offset );
66466645                                } break ;
66476646                            case  GGUF_TYPE_STRING :
66486647                                {
6649-                                     // prevent integer overflow in the malloc  below 
6648+                                     // prevent integer overflow in the calloc  below 
66506649                                    if  (kv -> value .arr .n  >= SIZE_MAX /sizeof (struct  gguf_str )) {
66516650                                        fprintf (stderr , "%s: array size is too large (%"  PRIu64  ")\n" , __func__ , kv -> value .arr .n );
66526651                                        fclose (file );
66536652                                        gguf_free (ctx );
66546653                                        return  NULL ;
66556654                                    }
66566655
6657-                                     kv -> value .arr .data  =  malloc (kv -> value .arr .n   *  sizeof (struct  gguf_str ));
6656+                                     kv -> value .arr .data  =  calloc (kv -> value .arr .n ,  sizeof (struct  gguf_str ));
66586657                                    if  (!kv -> value .arr .data ) {
66596658                                        fprintf (stderr , "%s: failed to allocate memory for array\n" , __func__ );
66606659                                        fclose (file );
@@ -7152,7 +7151,7 @@ static int gguf_get_or_add_key(struct gguf_context * ctx, const char * key) {
71527151    const  int  n_kv  =  gguf_get_n_kv (ctx );
71537152
71547153    ctx -> kv  =  realloc (ctx -> kv , (n_kv  +  1 ) *  sizeof (struct  gguf_kv ));
7155-     GGML_ASSERT (ctx -> kv ); // potential memory leak 
7154+     GGML_ASSERT (ctx -> kv ); // detect  potential memory leak 
71567155    memset (& ctx -> kv [n_kv ], 0 , sizeof (struct  gguf_kv ));
71577156    ctx -> kv [n_kv ].key .n     =  strlen (key );
71587157    ctx -> kv [n_kv ].key .data  =  strdup (key );
@@ -7170,7 +7169,7 @@ void gguf_remove_key(struct gguf_context * ctx, const char * key) {
71707169            ctx -> kv [i ] =  ctx -> kv [i + 1 ];
71717170        }
71727171        ctx -> kv  =  realloc (ctx -> kv , (n_kv  -  1 ) *  sizeof (struct  gguf_kv ));
7173-         GGML_ASSERT (ctx -> kv ); // potential memory leak 
7172+         GGML_ASSERT (ctx -> kv ); // detect  potential memory leak 
71747173        ctx -> header .n_kv -- ;
71757174    }
71767175}
@@ -7268,7 +7267,7 @@ void gguf_set_arr_data(struct gguf_context * ctx, const char * key, enum gguf_ty
72687267    ctx -> kv [idx ].value .arr .type  =  type ;
72697268    ctx -> kv [idx ].value .arr .n     =  n ;
72707269    ctx -> kv [idx ].value .arr .data  =  realloc (ctx -> kv [idx ].value .arr .data , nbytes );
7271-     GGML_ASSERT (ctx -> kv [idx ].value .arr .data ); // potential memory leak 
7270+     GGML_ASSERT (ctx -> kv [idx ].value .arr .data ); // detect  potential memory leak 
72727271    memcpy (ctx -> kv [idx ].value .arr .data , data , nbytes );
72737272}
72747273
@@ -7280,7 +7279,7 @@ void gguf_set_arr_str(struct gguf_context * ctx, const char * key, const char **
72807279    ctx -> kv [idx ].value .arr .type  =  GGUF_TYPE_STRING ;
72817280    ctx -> kv [idx ].value .arr .n     =  n ;
72827281    ctx -> kv [idx ].value .arr .data  =  realloc (ctx -> kv [idx ].value .arr .data , nbytes );
7283-     GGML_ASSERT (ctx -> kv [idx ].value .arr .data ); // potential memory leak 
7282+     GGML_ASSERT (ctx -> kv [idx ].value .arr .data ); // detect  potential memory leak 
72847283    for  (int  i  =  0 ; i  <  n ; ++ i ) {
72857284        struct  gguf_str  *  str  =  & ((struct  gguf_str  * )ctx -> kv [idx ].value .arr .data )[i ];
72867285        str -> n     =  strlen (data [i ]);
@@ -7308,7 +7307,7 @@ void gguf_set_kv(struct gguf_context * ctx, const struct gguf_context * src) {
73087307            case  GGUF_TYPE_ARRAY :
73097308                {
73107309                    if  (src -> kv [i ].value .arr .type  ==  GGUF_TYPE_STRING ) {
7311-                         const  char  * *  data  =  GGML_MALLOC (src -> kv [i ].value .arr .n   *  sizeof (char  * ));
7310+                         const  char  * *  data  =  GGML_CALLOC (src -> kv [i ].value .arr .n ,  sizeof (char  * ));
73127311                        for  (uint64_t  j  =  0 ; j  <  src -> kv [i ].value .arr .n ; ++ j ) {
73137312                            data [j ] =  ((struct  gguf_str  * )src -> kv [i ].value .arr .data )[j ].data ;
73147313                        }
@@ -7336,7 +7335,7 @@ void gguf_add_tensor(
73367335
73377336    const  uint64_t  idx  =  ctx -> header .n_tensors ;
73387337    ctx -> info  =  realloc (ctx -> info , (idx  +  1 )* sizeof (struct  gguf_tensor_info ));
7339-     GGML_ASSERT (ctx -> info ); // potential memory leak 
7338+     GGML_ASSERT (ctx -> info ); // detect  potential memory leak 
73407339    ctx -> info [idx ].t  =  * tensor ;
73417340    ctx -> info [idx ].offset  =  idx  ==  0  ? 0  :
73427341        ctx -> info [idx  -  1 ].offset  +  GGML_PAD (ggml_nbytes (& ctx -> info [idx  -  1 ].t ), ctx -> alignment );
@@ -7404,7 +7403,7 @@ static void gguf_buf_grow(struct gguf_buf * buf, size_t size) {
74047403        buf -> size  =  1.5f * (buf -> offset  +  size );
74057404        if  (buf -> data ) {
74067405            buf -> data  =  realloc (buf -> data , buf -> size );
7407-             GGML_ASSERT (buf -> data ); // potential memory leak 
7406+             GGML_ASSERT (buf -> data ); // detect  potential memory leak 
74087407        }
74097408    }
74107409}
0 commit comments