monitor-config-store: Fix incorrect string comparison with empty string

strncmp() always return 0 if the passed length is 0. What this means is
that whatever the first string check happens to be, if the parsed XML
cdata was empty (e.g. if we got <element></element>), the first
condition would evaluate to true, which is rather unexpected.

Fix this by making sure the string length is correct first. Also move it
into a helper so we don't need to repeat the same strlen() check every
time.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2030>
This commit is contained in:
Jonas Ådahl 2021-09-24 16:29:47 +02:00 committed by Marge Bot
parent b74e99b10b
commit 6c4380ed41

View File

@ -190,6 +190,17 @@ typedef struct
G_DEFINE_TYPE (MetaMonitorConfigStore, meta_monitor_config_store,
G_TYPE_OBJECT)
static gboolean
text_equals (const char *text,
int len,
const char *expect)
{
if (strlen (expect) != len)
return FALSE;
return strncmp (text, expect, len) == 0;
}
static void
enter_unknown_element (ConfigParser *parser,
const char *element_name,
@ -904,12 +915,12 @@ read_bool (const char *text,
gboolean *out_value,
GError **error)
{
if (strncmp (text, "no", text_len) == 0)
if (text_equals (text, text_len, "no"))
{
*out_value = FALSE;
return TRUE;
}
else if (strncmp (text, "yes", text_len) == 0)
else if (text_equals (text, text_len, "yes"))
{
*out_value = TRUE;
return TRUE;
@ -1039,13 +1050,13 @@ handle_text (GMarkupParseContext *context,
case STATE_TRANSFORM_ROTATION:
{
if (strncmp (text, "normal", text_len) == 0)
if (text_equals (text, text_len, "normal"))
parser->current_transform = META_MONITOR_TRANSFORM_NORMAL;
else if (strncmp (text, "left", text_len) == 0)
else if (text_equals (text, text_len, "left"))
parser->current_transform = META_MONITOR_TRANSFORM_90;
else if (strncmp (text, "upside_down", text_len) == 0)
else if (text_equals (text, text_len, "upside_down"))
parser->current_transform = META_MONITOR_TRANSFORM_180;
else if (strncmp (text, "right", text_len) == 0)
else if (text_equals (text, text_len, "right"))
parser->current_transform = META_MONITOR_TRANSFORM_270;
else
g_set_error (error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
@ -1088,7 +1099,7 @@ handle_text (GMarkupParseContext *context,
case STATE_MONITOR_MODE_FLAG:
{
if (strncmp (text, "interlace", text_len) == 0)
if (text_equals (text, text_len, "interlace"))
{
parser->current_monitor_mode_spec->flags |=
META_CRTC_MODE_FLAG_INTERLACE;