properties: log a warning if the property type differs

If we try to fetch a property as some specific type and the parsing
fails, log a warning. This should catch any client bugs where the
properties are set wrongly.
This commit is contained in:
Peter Hutterer 2021-10-13 13:32:07 +10:00 committed by Wim Taymans
parent 32fb369b66
commit eabd00e012
2 changed files with 30 additions and 4 deletions

View file

@ -66,6 +66,7 @@ PW_LOG_TOPIC(log_metadata, "pw.metadata");
PW_LOG_TOPIC(log_module, "pw.module");
PW_LOG_TOPIC(log_node, "pw.node");
PW_LOG_TOPIC(log_port, "pw.port");
PW_LOG_TOPIC(log_properties, "pw.props");
PW_LOG_TOPIC(log_protocol, "pw.protocol");
PW_LOG_TOPIC(log_proxy, "pw.proxy");
PW_LOG_TOPIC(log_resource, "pw.resource");
@ -445,6 +446,7 @@ pw_log_init(void)
PW_LOG_TOPIC_INIT(log_module);
PW_LOG_TOPIC_INIT(log_node);
PW_LOG_TOPIC_INIT(log_port);
PW_LOG_TOPIC_INIT(log_properties);
PW_LOG_TOPIC_INIT(log_protocol);
PW_LOG_TOPIC_INIT(log_proxy);
PW_LOG_TOPIC_INIT(log_resource);

View file

@ -28,9 +28,13 @@
#include <spa/utils/string.h>
#include "pipewire/array.h"
#include "pipewire/log.h"
#include "pipewire/utils.h"
#include "pipewire/properties.h"
PW_LOG_TOPIC_EXTERN(log_properties);
#define PW_LOG_TOPIC_DEFAULT log_properties
/** \cond */
struct properties {
struct pw_properties this;
@ -510,11 +514,16 @@ int pw_properties_fetch_uint32(const struct pw_properties *properties, const cha
uint32_t *value)
{
const char *str = pw_properties_get(properties, key);
bool success;
if (!str)
return -ENOENT;
return spa_atou32(str, value, 0) ? 0 : -EINVAL;
success = spa_atou32(str, value, 0);
if (SPA_UNLIKELY(!success))
pw_log_warn("Failed to parse \"%s\"=\"%s\" as int32", key, str);
return success ? 0 : -EINVAL;
}
/** Fetch a property as int32_t
@ -532,11 +541,16 @@ int pw_properties_fetch_int32(const struct pw_properties *properties, const char
int32_t *value)
{
const char *str = pw_properties_get(properties, key);
bool success;
if (!str)
return -ENOENT;
return spa_atoi32(str, value, 0) ? 0 : -EINVAL;
success = spa_atoi32(str, value, 0);
if (SPA_UNLIKELY(!success))
pw_log_warn("Failed to parse \"%s\"=\"%s\" as int32", key, str);
return success ? 0 : -EINVAL;
}
/** Fetch a property as uint64_t.
@ -554,11 +568,16 @@ int pw_properties_fetch_uint64(const struct pw_properties *properties, const cha
uint64_t *value)
{
const char *str = pw_properties_get(properties, key);
bool success;
if (!str)
return -ENOENT;
return spa_atou64(str, value, 0) ? 0 : -EINVAL;
success = spa_atou64(str, value, 0);
if (SPA_UNLIKELY(!success))
pw_log_warn("Failed to parse \"%s\"=\"%s\" as uint64", key, str);
return success ? 0 : -EINVAL;
}
/** Fetch a property as int64_t
@ -576,11 +595,16 @@ int pw_properties_fetch_int64(const struct pw_properties *properties, const char
int64_t *value)
{
const char *str = pw_properties_get(properties, key);
bool success;
if (!str)
return -ENOENT;
return spa_atoi64(str, value, 0) ? 0 : -EINVAL;
success = spa_atoi64(str, value, 0);
if (SPA_UNLIKELY(!success))
pw_log_warn("Failed to parse \"%s\"=\"%s\" as int64", key, str);
return success ? 0 : -EINVAL;
}
/** Fetch a property as boolean value