Spaces:
Sleeping
Sleeping
Merge pull request from GHSA-p5mv-gjc5-mwqv
Browse files* always use calloc
clamp n_kv on failure to read a kv
* ggml : alternative ctx->header.n_kv update
---------
Co-authored-by: slaren <[email protected]>
ggml.c
CHANGED
|
@@ -20685,7 +20685,7 @@ static void gguf_free_kv(struct gguf_kv * kv) {
|
|
| 20685 |
}
|
| 20686 |
|
| 20687 |
struct gguf_context * gguf_init_empty(void) {
|
| 20688 |
-
struct gguf_context * ctx =
|
| 20689 |
|
| 20690 |
memcpy(ctx->header.magic, GGUF_MAGIC, sizeof(ctx->header.magic));
|
| 20691 |
ctx->header.version = GGUF_VERSION;
|
|
@@ -20730,7 +20730,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
|
|
| 20730 |
|
| 20731 |
bool ok = true;
|
| 20732 |
|
| 20733 |
-
struct gguf_context * ctx =
|
| 20734 |
|
| 20735 |
// read the header
|
| 20736 |
{
|
|
@@ -20767,9 +20767,13 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
|
|
| 20767 |
|
| 20768 |
// read the kv pairs
|
| 20769 |
{
|
| 20770 |
-
|
| 20771 |
|
| 20772 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20773 |
struct gguf_kv * kv = &ctx->kv[i];
|
| 20774 |
|
| 20775 |
//fprintf(stderr, "%s: reading kv %d\n", __func__, i);
|
|
@@ -20818,7 +20822,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
|
|
| 20818 |
return NULL;
|
| 20819 |
}
|
| 20820 |
|
| 20821 |
-
kv->value.arr.data =
|
| 20822 |
|
| 20823 |
ok = ok && gguf_fread_el(file, kv->value.arr.data, kv->value.arr.n * gguf_type_size(kv->value.arr.type), &offset);
|
| 20824 |
} break;
|
|
@@ -20832,7 +20836,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
|
|
| 20832 |
return NULL;
|
| 20833 |
}
|
| 20834 |
|
| 20835 |
-
kv->value.arr.data =
|
| 20836 |
|
| 20837 |
for (uint64_t j = 0; j < kv->value.arr.n; ++j) {
|
| 20838 |
ok = ok && gguf_fread_str(file, &((struct gguf_str *) kv->value.arr.data)[j], &offset);
|
|
@@ -20848,6 +20852,8 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
|
|
| 20848 |
if (!ok) {
|
| 20849 |
break;
|
| 20850 |
}
|
|
|
|
|
|
|
| 20851 |
}
|
| 20852 |
|
| 20853 |
if (!ok) {
|
|
@@ -20860,7 +20866,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
|
|
| 20860 |
|
| 20861 |
// read the tensor infos
|
| 20862 |
{
|
| 20863 |
-
ctx->infos =
|
| 20864 |
|
| 20865 |
for (uint64_t i = 0; i < ctx->header.n_tensors; ++i) {
|
| 20866 |
struct gguf_tensor_info * info = &ctx->infos[i];
|
|
@@ -20881,6 +20887,7 @@ struct gguf_context * gguf_init_from_file(const char * fname, struct gguf_init_p
|
|
| 20881 |
ok = ok && gguf_fread_el (file, &info->type, sizeof(info->type), &offset);
|
| 20882 |
ok = ok && gguf_fread_el (file, &info->offset, sizeof(info->offset), &offset);
|
| 20883 |
|
|
|
|
| 20884 |
gguf_tensor_info_sanitize(info);
|
| 20885 |
|
| 20886 |
if (!ok) {
|
|
@@ -21362,7 +21369,7 @@ void gguf_set_arr_data(struct gguf_context * ctx, const char * key, enum gguf_ty
|
|
| 21362 |
ctx->kv[idx].type = GGUF_TYPE_ARRAY;
|
| 21363 |
ctx->kv[idx].value.arr.type = type;
|
| 21364 |
ctx->kv[idx].value.arr.n = n;
|
| 21365 |
-
ctx->kv[idx].value.arr.data =
|
| 21366 |
memcpy(ctx->kv[idx].value.arr.data, data, n*gguf_type_size(type));
|
| 21367 |
}
|
| 21368 |
|
|
@@ -21372,7 +21379,7 @@ void gguf_set_arr_str(struct gguf_context * ctx, const char * key, const char **
|
|
| 21372 |
ctx->kv[idx].type = GGUF_TYPE_ARRAY;
|
| 21373 |
ctx->kv[idx].value.arr.type = GGUF_TYPE_STRING;
|
| 21374 |
ctx->kv[idx].value.arr.n = n;
|
| 21375 |
-
ctx->kv[idx].value.arr.data =
|
| 21376 |
for (int i = 0; i < n; i++) {
|
| 21377 |
struct gguf_str * str = &((struct gguf_str *)ctx->kv[idx].value.arr.data)[i];
|
| 21378 |
str->n = strlen(data[i]);
|
|
@@ -21399,7 +21406,7 @@ void gguf_set_kv(struct gguf_context * ctx, struct gguf_context * src) {
|
|
| 21399 |
case GGUF_TYPE_ARRAY:
|
| 21400 |
{
|
| 21401 |
if (src->kv[i].value.arr.type == GGUF_TYPE_STRING) {
|
| 21402 |
-
const char ** data =
|
| 21403 |
for (uint32_t j = 0; j < src->kv[i].value.arr.n; j++) {
|
| 21404 |
data[j] = ((struct gguf_str *)src->kv[i].value.arr.data)[j].data;
|
| 21405 |
}
|
|
@@ -21487,7 +21494,7 @@ struct gguf_buf {
|
|
| 21487 |
|
| 21488 |
static struct gguf_buf gguf_buf_init(size_t size) {
|
| 21489 |
struct gguf_buf buf = {
|
| 21490 |
-
/*buf.data =*/ size == 0 ? NULL :
|
| 21491 |
/*buf.size =*/ size,
|
| 21492 |
/*buf.offset =*/ 0,
|
| 21493 |
};
|
|
|
|
| 20685 |
}
|
| 20686 |
|
| 20687 |
struct gguf_context * gguf_init_empty(void) {
|
| 20688 |
+
struct gguf_context * ctx = GGML_CALLOC(1, sizeof(struct gguf_context));
|
| 20689 |
|
| 20690 |
memcpy(ctx->header.magic, GGUF_MAGIC, sizeof(ctx->header.magic));
|
| 20691 |
ctx->header.version = GGUF_VERSION;
|
|
|
|
| 20730 |
|
| 20731 |
bool ok = true;
|
| 20732 |
|
| 20733 |
+
struct gguf_context * ctx = GGML_CALLOC(1, sizeof(struct gguf_context));
|
| 20734 |
|
| 20735 |
// read the header
|
| 20736 |
{
|
|
|
|
| 20767 |
|
| 20768 |
// read the kv pairs
|
| 20769 |
{
|
| 20770 |
+
const uint64_t n_kv = ctx->header.n_kv;
|
| 20771 |
|
| 20772 |
+
// header.n_kv will hold the actual value of pairs that were successfully read in the loop below
|
| 20773 |
+
ctx->header.n_kv = 0;
|
| 20774 |
+
ctx->kv = GGML_CALLOC(n_kv, sizeof(struct gguf_kv));
|
| 20775 |
+
|
| 20776 |
+
for (uint64_t i = 0; i < n_kv; ++i) {
|
| 20777 |
struct gguf_kv * kv = &ctx->kv[i];
|
| 20778 |
|
| 20779 |
//fprintf(stderr, "%s: reading kv %d\n", __func__, i);
|
|
|
|
| 20822 |
return NULL;
|
| 20823 |
}
|
| 20824 |
|
| 20825 |
+
kv->value.arr.data = GGML_CALLOC(kv->value.arr.n, gguf_type_size(kv->value.arr.type));
|
| 20826 |
|
| 20827 |
ok = ok && gguf_fread_el(file, kv->value.arr.data, kv->value.arr.n * gguf_type_size(kv->value.arr.type), &offset);
|
| 20828 |
} break;
|
|
|
|
| 20836 |
return NULL;
|
| 20837 |
}
|
| 20838 |
|
| 20839 |
+
kv->value.arr.data = GGML_CALLOC(kv->value.arr.n, sizeof(struct gguf_str));
|
| 20840 |
|
| 20841 |
for (uint64_t j = 0; j < kv->value.arr.n; ++j) {
|
| 20842 |
ok = ok && gguf_fread_str(file, &((struct gguf_str *) kv->value.arr.data)[j], &offset);
|
|
|
|
| 20852 |
if (!ok) {
|
| 20853 |
break;
|
| 20854 |
}
|
| 20855 |
+
|
| 20856 |
+
ctx->header.n_kv++;
|
| 20857 |
}
|
| 20858 |
|
| 20859 |
if (!ok) {
|
|
|
|
| 20866 |
|
| 20867 |
// read the tensor infos
|
| 20868 |
{
|
| 20869 |
+
ctx->infos = GGML_CALLOC(ctx->header.n_tensors, sizeof(struct gguf_tensor_info));
|
| 20870 |
|
| 20871 |
for (uint64_t i = 0; i < ctx->header.n_tensors; ++i) {
|
| 20872 |
struct gguf_tensor_info * info = &ctx->infos[i];
|
|
|
|
| 20887 |
ok = ok && gguf_fread_el (file, &info->type, sizeof(info->type), &offset);
|
| 20888 |
ok = ok && gguf_fread_el (file, &info->offset, sizeof(info->offset), &offset);
|
| 20889 |
|
| 20890 |
+
// TODO: return an error instead of crashing with GGML_ASSERT
|
| 20891 |
gguf_tensor_info_sanitize(info);
|
| 20892 |
|
| 20893 |
if (!ok) {
|
|
|
|
| 21369 |
ctx->kv[idx].type = GGUF_TYPE_ARRAY;
|
| 21370 |
ctx->kv[idx].value.arr.type = type;
|
| 21371 |
ctx->kv[idx].value.arr.n = n;
|
| 21372 |
+
ctx->kv[idx].value.arr.data = GGML_CALLOC(n, gguf_type_size(type));
|
| 21373 |
memcpy(ctx->kv[idx].value.arr.data, data, n*gguf_type_size(type));
|
| 21374 |
}
|
| 21375 |
|
|
|
|
| 21379 |
ctx->kv[idx].type = GGUF_TYPE_ARRAY;
|
| 21380 |
ctx->kv[idx].value.arr.type = GGUF_TYPE_STRING;
|
| 21381 |
ctx->kv[idx].value.arr.n = n;
|
| 21382 |
+
ctx->kv[idx].value.arr.data = GGML_CALLOC(n, sizeof(struct gguf_str));
|
| 21383 |
for (int i = 0; i < n; i++) {
|
| 21384 |
struct gguf_str * str = &((struct gguf_str *)ctx->kv[idx].value.arr.data)[i];
|
| 21385 |
str->n = strlen(data[i]);
|
|
|
|
| 21406 |
case GGUF_TYPE_ARRAY:
|
| 21407 |
{
|
| 21408 |
if (src->kv[i].value.arr.type == GGUF_TYPE_STRING) {
|
| 21409 |
+
const char ** data = GGML_CALLOC(src->kv[i].value.arr.n, sizeof(char *));
|
| 21410 |
for (uint32_t j = 0; j < src->kv[i].value.arr.n; j++) {
|
| 21411 |
data[j] = ((struct gguf_str *)src->kv[i].value.arr.data)[j].data;
|
| 21412 |
}
|
|
|
|
| 21494 |
|
| 21495 |
static struct gguf_buf gguf_buf_init(size_t size) {
|
| 21496 |
struct gguf_buf buf = {
|
| 21497 |
+
/*buf.data =*/ size == 0 ? NULL : GGML_CALLOC(1, size),
|
| 21498 |
/*buf.size =*/ size,
|
| 21499 |
/*buf.offset =*/ 0,
|
| 21500 |
};
|