Skip to content

Commit d4a4540

Browse files
authored
Fix buffer corruption in metadata retrieval for function type (#35)
* fix buffer corruption in metadata function arg type * use snprintf instead of sprintf
1 parent ecd807b commit d4a4540

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/thingset.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -739,27 +739,27 @@ static inline char *type_to_type_name(const enum thingset_type type)
739739
static int get_function_arg_types(struct thingset_context *ts, uint16_t parent_id, char *buf,
740740
size_t size)
741741
{
742-
int len = 0;
742+
int total_len = 0;
743743
for (unsigned int i = 0; i < ts->num_objects; i++) {
744744
if (ts->data_objects[i].parent_id == parent_id) {
745-
if (len > 0) {
745+
int len = 0;
746+
if (total_len > 0) {
746747
if (size < 2) {
747748
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
748749
}
749-
len += sprintf(buf, ",");
750-
size -= 1;
751-
buf += 1;
750+
len += snprintf(buf, size, ",");
752751
}
753752
char *elementType = type_to_type_name(ts->data_objects[i].type);
754-
if (len > size) {
755-
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
756-
}
757-
len += sprintf(buf, "%s", elementType);
753+
len += snprintf(buf + len, size - len, "%s", elementType);
758754
buf += len;
759755
size -= len;
756+
total_len += len;
757+
if (total_len > size) {
758+
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
759+
}
760760
}
761761
}
762-
return len;
762+
return total_len;
763763
}
764764

765765
int thingset_get_type_name(struct thingset_context *ts, const struct thingset_data_object *obj,
@@ -771,11 +771,11 @@ int thingset_get_type_name(struct thingset_context *ts, const struct thingset_da
771771
if (sizeof(elementType) > size) {
772772
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
773773
}
774-
return sprintf(buf, "%s[]", elementType);
774+
return snprintf(buf, size, "%s[]", elementType);
775775
}
776776
case THINGSET_TYPE_FN_VOID:
777777
case THINGSET_TYPE_FN_I32:
778-
sprintf(buf, "(");
778+
snprintf(buf, size, "(");
779779
int len = 1 + get_function_arg_types(ts, obj->id, buf + 1, size - 1);
780780
if (len < 0) {
781781
return -THINGSET_ERR_RESPONSE_TOO_LARGE;
@@ -787,18 +787,18 @@ int thingset_get_type_name(struct thingset_context *ts, const struct thingset_da
787787
size -= len;
788788
switch (obj->type) {
789789
case THINGSET_TYPE_FN_VOID:
790-
len += sprintf(buf, ")->()");
790+
len += snprintf(buf, size, ")->()");
791791
break;
792792
case THINGSET_TYPE_FN_I32:
793-
len += sprintf(buf, ")->(i32)");
793+
len += snprintf(buf, size, ")->(i32)");
794794
break;
795795
default:
796796
break;
797797
}
798798
return len;
799799
default: {
800800
char *type = type_to_type_name(obj->type);
801-
return sprintf(buf, "%s", type);
801+
return snprintf(buf, size, "%s", type);
802802
}
803803
}
804804
}

0 commit comments

Comments
 (0)