json: handle overflow better

We need at least the length of the string+1 as the length of the
target in spa_json_get_string(). Add a unit test for this.
This commit is contained in:
Wim Taymans 2021-03-18 19:44:25 +01:00
parent fc9a6d6b1e
commit db85339f50
2 changed files with 18 additions and 1 deletions

View file

@ -194,11 +194,28 @@ static void test_arrays(void)
test_array("[ FL FR ]", (char *[]){ "FL", "FR", NULL });
}
static void test_overflow(void)
{
struct spa_json it[2];
char val[3];
const char *str = "[ F, FR, FRC ]";
spa_json_init(&it[0], str, strlen(str));
spa_assert(spa_json_enter_array(&it[0], &it[1]) > 0);
spa_assert(spa_json_get_string(&it[1], val, sizeof(val)) > 0);
spa_assert(strcmp(val, "F") == 0);
spa_assert(spa_json_get_string(&it[1], val, sizeof(val)) > 0);
spa_assert(strcmp(val, "FR") == 0);
spa_assert(spa_json_get_string(&it[1], val, sizeof(val)) < 0);
}
int main(int argc, char *argv[])
{
test_abi();
test_parse();
test_encode();
test_arrays();
test_overflow();
return 0;
}