spa: spa_json_get_string does not return the length

The returned string is null terminated.
This commit is contained in:
Wim Taymans 2021-03-18 18:57:26 +01:00
parent 8e590df92f
commit fc9a6d6b1e
4 changed files with 15 additions and 17 deletions

View file

@ -207,11 +207,11 @@ static inline uint32_t spa_alsa_format_from_name(const char *name, size_t len)
return SPA_AUDIO_FORMAT_UNKNOWN;
}
static inline uint32_t spa_alsa_channel_from_name(const char *name, size_t len)
static inline uint32_t spa_alsa_channel_from_name(const char *name)
{
int i;
for (i = 0; spa_type_audio_channel[i].name; i++) {
if (strncmp(name, spa_debug_type_short_name(spa_type_audio_channel[i].name), len) == 0)
if (strcmp(name, spa_debug_type_short_name(spa_type_audio_channel[i].name)) == 0)
return spa_type_audio_channel[i].type;
}
return SPA_AUDIO_CHANNEL_UNKNOWN;
@ -221,16 +221,15 @@ static inline void spa_alsa_parse_position(struct channel_map *map, const char *
{
struct spa_json it[2];
char v[256];
int l;
spa_json_init(&it[0], val, len);
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
spa_json_init(&it[1], val, len);
map->channels = 0;
while ((l = spa_json_get_string(&it[1], v, sizeof(v))) > 0 &&
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 &&
map->channels < SPA_AUDIO_MAX_CHANNELS) {
map->pos[map->channels++] = spa_alsa_channel_from_name(v, l);
map->pos[map->channels++] = spa_alsa_channel_from_name(v);
}
}

View file

@ -1226,11 +1226,11 @@ impl_get_size(const struct spa_handle_factory *factory,
return sizeof(struct impl);
}
static uint32_t channel_from_name(const char *name, size_t len)
static uint32_t channel_from_name(const char *name)
{
int i;
for (i = 0; spa_type_audio_channel[i].name; i++) {
if (strncmp(name, spa_debug_type_short_name(spa_type_audio_channel[i].name), len) == 0)
if (strcmp(name, spa_debug_type_short_name(spa_type_audio_channel[i].name)) == 0)
return spa_type_audio_channel[i].type;
}
return SPA_AUDIO_CHANNEL_UNKNOWN;
@ -1240,16 +1240,15 @@ static inline uint32_t parse_position(uint32_t *pos, const char *val, size_t len
{
struct spa_json it[2];
char v[256];
int l;
uint32_t i = 0;
spa_json_init(&it[0], val, len);
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
spa_json_init(&it[1], val, len);
while ((l = spa_json_get_string(&it[1], v, sizeof(v))) > 0 &&
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 &&
i < SPA_AUDIO_MAX_CHANNELS) {
pos[i++] = channel_from_name(v, l);
pos[i++] = channel_from_name(v);
}
return i;
}

View file

@ -724,11 +724,11 @@ impl_get_size(const struct spa_handle_factory *factory,
return sizeof(struct impl);
}
static uint32_t channel_from_name(const char *name, size_t len)
static uint32_t channel_from_name(const char *name)
{
int i;
for (i = 0; spa_type_audio_channel[i].name; i++) {
if (strncmp(name, spa_debug_type_short_name(spa_type_audio_channel[i].name), len) == 0)
if (strcmp(name, spa_debug_type_short_name(spa_type_audio_channel[i].name)) == 0)
return spa_type_audio_channel[i].type;
}
return SPA_AUDIO_CHANNEL_UNKNOWN;
@ -738,16 +738,15 @@ static inline void parse_position(struct impl *this, const char *val, size_t len
{
struct spa_json it[2];
char v[256];
int l;
spa_json_init(&it[0], val, len);
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
spa_json_init(&it[1], val, len);
this->props.n_pos = 0;
while ((l = spa_json_get_string(&it[1], v, sizeof(v))) > 0 &&
while (spa_json_get_string(&it[1], v, sizeof(v)) > 0 &&
this->props.n_pos < SPA_AUDIO_MAX_CHANNELS) {
this->props.pos[this->props.n_pos++] = channel_from_name(v, l);
this->props.pos[this->props.n_pos++] = channel_from_name(v);
}
}

View file

@ -173,13 +173,13 @@ static void test_array(char *str, char **vals)
{
struct spa_json it[2];
char val[256];
int i, len;
int i;
spa_json_init(&it[0], str, strlen(str));
if (spa_json_enter_array(&it[0], &it[1]) <= 0)
spa_json_init(&it[1], str, strlen(str));
for (i = 0; vals[i]; i++) {
spa_assert((len = spa_json_get_string(&it[1], val, sizeof(val))) > 0);
spa_assert(spa_json_get_string(&it[1], val, sizeof(val)) > 0);
spa_assert(strcmp(val, vals[i]) == 0);
}
}
@ -191,6 +191,7 @@ static void test_arrays(void)
test_array("[ FL , FR ]", (char *[]){ "FL", "FR", NULL });
test_array("[FL FR]", (char *[]){ "FL", "FR", NULL });
test_array("FL FR", (char *[]){ "FL", "FR", NULL });
test_array("[ FL FR ]", (char *[]){ "FL", "FR", NULL });
}
int main(int argc, char *argv[])