pod: check string zero byte only when parsing

The _is_type() macros should simply check the type in the header and if
the size is large enough to look into the type specifics. Further
validation of the values should be done when the value is retrieved.

Following this logic, the String zero byte check should be done in the
get_string() function.
This commit is contained in:
Wim Taymans 2025-07-25 17:01:24 +02:00
parent 9e789c65c2
commit b991e9acc9

View file

@ -279,24 +279,28 @@ SPA_API_POD_ITER int spa_pod_get_double(const struct spa_pod *pod, double *value
SPA_API_POD_ITER int spa_pod_is_string(const struct spa_pod *pod) SPA_API_POD_ITER int spa_pod_is_string(const struct spa_pod *pod)
{ {
const char *s = (const char *)SPA_POD_CONTENTS(struct spa_pod_string, pod); return SPA_POD_CHECK(pod, SPA_TYPE_String, 1);
return SPA_POD_CHECK(pod, SPA_TYPE_String, 1) &&
s[pod->size-1] == '\0';
} }
SPA_API_POD_ITER int spa_pod_get_string(const struct spa_pod *pod, const char **value) SPA_API_POD_ITER int spa_pod_get_string(const struct spa_pod *pod, const char **value)
{ {
const char *s;
if (!spa_pod_is_string(pod)) if (!spa_pod_is_string(pod))
return -EINVAL; return -EINVAL;
*value = (const char *)SPA_POD_CONTENTS(struct spa_pod_string, pod); s = (const char *)SPA_POD_CONTENTS(struct spa_pod_string, pod);
if (s[pod->size-1] != '\0')
return -EINVAL;
*value = s;
return 0; return 0;
} }
SPA_API_POD_ITER int spa_pod_copy_string(const struct spa_pod *pod, size_t maxlen, char *dest) SPA_API_POD_ITER int spa_pod_copy_string(const struct spa_pod *pod, size_t maxlen, char *dest)
{ {
const char *s = (const char *)SPA_POD_CONTENTS(struct spa_pod_string, pod); const char *s;
if (!spa_pod_is_string(pod) || maxlen < 1) if (!spa_pod_is_string(pod) || maxlen < 1)
return -EINVAL; return -EINVAL;
maxlen = SPA_MIN(maxlen, pod->size);
s = (const char *)SPA_POD_CONTENTS(struct spa_pod_string, pod);
strncpy(dest, s, maxlen-1); strncpy(dest, s, maxlen-1);
dest[maxlen-1]= '\0'; dest[maxlen-1]= '\0';
return 0; return 0;