@@ -22136,7 +22136,11 @@ static bool gguf_fread_str(FILE * file, struct gguf_str * p, size_t * offset) {
22136
22136
return false;
22137
22137
}
22138
22138
22139
- p->data = GGML_CALLOC(p->n + 1, 1);
22139
+ p->data = calloc(p->n + 1, 1);
22140
+ if (!p->data) {
22141
+ fprintf(stderr, "%s: failed to allocate memory for string of length %" PRIu64 "\n", __func__, p->n);
22142
+ return false;
22143
+ }
22140
22144
22141
22145
ok = ok && gguf_fread_el(file, p->data, p->n, offset);
22142
22146
@@ -22170,7 +22174,11 @@ static void gguf_free_kv(struct gguf_kv * kv) {
22170
22174
}
22171
22175
22172
22176
struct gguf_context * gguf_init_empty(void) {
22173
- struct gguf_context * ctx = GGML_CALLOC(1, sizeof(struct gguf_context));
22177
+ struct gguf_context * ctx = calloc(1, sizeof(struct gguf_context));
22178
+ if (!ctx) {
22179
+ fprintf(stderr, "%s: failed to allocate memory for context\n", __func__);
22180
+ return NULL;
22181
+ }
22174
22182
22175
22183
memcpy(ctx->header.magic, GGUF_MAGIC, sizeof(ctx->header.magic));
22176
22184
ctx->header.version = GGUF_VERSION;
@@ -22216,7 +22224,12 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
22216
22224
22217
22225
bool ok = true;
22218
22226
22219
- struct gguf_context * ctx = GGML_CALLOC(1, sizeof(struct gguf_context));
22227
+ struct gguf_context * ctx = calloc(1, sizeof(struct gguf_context));
22228
+ if (!ctx) {
22229
+ fprintf(stderr, "%s: failed to allocate memory for context\n", __func__);
22230
+ fclose(file);
22231
+ return NULL;
22232
+ }
22220
22233
22221
22234
// read the header
22222
22235
{
@@ -22255,9 +22268,13 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
22255
22268
{
22256
22269
const uint64_t n_kv = ctx->header.n_kv;
22257
22270
22258
- // header.n_kv will hold the actual value of pairs that were successfully read in the loop below
22259
- ctx->header.n_kv = 0;
22260
- ctx->kv = GGML_CALLOC(n_kv, sizeof(struct gguf_kv));
22271
+ ctx->kv = calloc(n_kv, sizeof(struct gguf_kv));
22272
+ if (!ctx->kv) {
22273
+ fprintf(stderr, "%s: failed to allocate memory for kv pairs\n", __func__);
22274
+ fclose(file);
22275
+ gguf_free(ctx);
22276
+ return NULL;
22277
+ }
22261
22278
22262
22279
for (uint64_t i = 0; i < n_kv; ++i) {
22263
22280
struct gguf_kv * kv = &ctx->kv[i];
@@ -22308,7 +22325,13 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
22308
22325
return NULL;
22309
22326
}
22310
22327
22311
- kv->value.arr.data = GGML_CALLOC(kv->value.arr.n, gguf_type_size(kv->value.arr.type));
22328
+ kv->value.arr.data = calloc(kv->value.arr.n, gguf_type_size(kv->value.arr.type));
22329
+ if (!kv->value.arr.data) {
22330
+ fprintf(stderr, "%s: failed to allocate memory for array\n", __func__);
22331
+ fclose(file);
22332
+ gguf_free(ctx);
22333
+ return NULL;
22334
+ }
22312
22335
22313
22336
ok = ok && gguf_fread_el(file, kv->value.arr.data, kv->value.arr.n * gguf_type_size(kv->value.arr.type), &offset);
22314
22337
} break;
@@ -22322,24 +22345,36 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
22322
22345
return NULL;
22323
22346
}
22324
22347
22325
- kv->value.arr.data = GGML_CALLOC(kv->value.arr.n, sizeof(struct gguf_str));
22348
+ kv->value.arr.data = calloc(kv->value.arr.n, sizeof(struct gguf_str));
22349
+ if (!kv->value.arr.data) {
22350
+ fprintf(stderr, "%s: failed to allocate memory for array\n", __func__);
22351
+ fclose(file);
22352
+ gguf_free(ctx);
22353
+ return NULL;
22354
+ }
22326
22355
22327
22356
for (uint64_t j = 0; j < kv->value.arr.n; ++j) {
22328
22357
ok = ok && gguf_fread_str(file, &((struct gguf_str *) kv->value.arr.data)[j], &offset);
22329
22358
}
22330
22359
} break;
22331
22360
case GGUF_TYPE_ARRAY:
22332
- default: GGML_ABORT("invalid type");
22361
+ default:
22362
+ {
22363
+ fprintf(stderr, "%s: invalid array type %d\n", __func__, kv->value.arr.type);
22364
+ ok = false;
22365
+ } break;
22333
22366
}
22334
22367
} break;
22335
- default: GGML_ABORT("invalid type");
22368
+ default:
22369
+ {
22370
+ fprintf(stderr, "%s: invalid type %d\n", __func__, kv->type);
22371
+ ok = false;
22372
+ } break;
22336
22373
}
22337
22374
22338
22375
if (!ok) {
22339
22376
break;
22340
22377
}
22341
-
22342
- ctx->header.n_kv++;
22343
22378
}
22344
22379
22345
22380
if (!ok) {
@@ -22352,7 +22387,13 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
22352
22387
22353
22388
// read the tensor infos
22354
22389
if (ctx->header.n_tensors > 0) {
22355
- ctx->infos = GGML_CALLOC(ctx->header.n_tensors, sizeof(struct gguf_tensor_info));
22390
+ ctx->infos = calloc(ctx->header.n_tensors, sizeof(struct gguf_tensor_info));
22391
+ if (!ctx->infos) {
22392
+ fprintf(stderr, "%s: failed to allocate memory for tensor infos\n", __func__);
22393
+ fclose(file);
22394
+ gguf_free(ctx);
22395
+ return NULL;
22396
+ }
22356
22397
22357
22398
for (uint64_t i = 0; i < ctx->header.n_tensors; ++i) {
22358
22399
struct gguf_tensor_info * info = &ctx->infos[i];
0 commit comments