diff options
author | Richard Hughes <richard@hughsie.com> | 2008-11-07 13:51:00 +0000 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2008-11-07 13:51:00 +0000 |
commit | 67c7fb3cda6746fad3a20516f96809743a0ee57a (patch) | |
tree | b8895c3dc0d4633b5f9dd33e02dfdc4b6fe30325 | |
parent | 7d87461bb3adb59588c8b80f747937058b76489c (diff) |
trivial: more checking of the input from the WUP device
-rw-r--r-- | src/dkp-wup.c | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/src/dkp-wup.c b/src/dkp-wup.c index 7e006af..41acf7e 100644 --- a/src/dkp-wup.c +++ b/src/dkp-wup.c @@ -240,7 +240,7 @@ dkp_wup_parse_command (DkpWup *wup, const gchar *data) guint i; guint size; guint length; - guint length_tok; + guint number_tokens; DkpDevice *device = DKP_DEVICE (wup); DkpObject *obj = dkp_device_get_obj (device); const guint offset = 3; @@ -283,19 +283,19 @@ dkp_wup_parse_command (DkpWup *wup, const gchar *data) /* check we have enough data inthe packet */ tokens = g_strsplit (packet, ",", -1); - length = g_strv_length (tokens); - if (length < 3) { + number_tokens = g_strv_length (tokens); + if (number_tokens < 3) { egg_debug ("not enough tokens '%s'", packet); goto out; } /* remove leading or trailing whitespace in tokens */ - for (i=0; i<length; i++) + for (i=0; i<number_tokens; i++) g_strstrip (tokens[i]); /* check the first token */ - length_tok = strlen (tokens[0]); - if (length_tok != 2) { + length = strlen (tokens[0]); + if (length != 2) { egg_debug ("expected command '#?' but got '%s'", tokens[0]); goto out; } @@ -306,17 +306,24 @@ dkp_wup_parse_command (DkpWup *wup, const gchar *data) command = tokens[0][1]; /* check the second token */ - length_tok = strlen (tokens[1]); - if (length_tok != 1) { + length = strlen (tokens[1]); + if (length != 1) { egg_debug ("expected command '?' but got '%s'", tokens[1]); goto out; } subcommand = tokens[1][0]; /* expect to be '-' */ - /* check the length */ + /* check the length is present */ + length = strlen (tokens[2]); + if (length == 0) { + egg_debug ("length value not present"); + goto out; + } + + /* check the length matches what data we've got*/ size = atoi (tokens[2]); - if (size != length - offset) { - egg_debug ("size expected to be '%i' but got '%i'", length - offset, size); + if (size != number_tokens - offset) { + egg_debug ("size expected to be '%i' but got '%i'", number_tokens - offset, size); goto out; } @@ -329,7 +336,7 @@ dkp_wup_parse_command (DkpWup *wup, const gchar *data) #endif /* update the command fields */ - if (command == 'd' && subcommand == '-') { + if (command == 'd' && subcommand == '-' && number_tokens - offset == 18) { obj->energy_rate = strtod (tokens[offset+DKP_WUP_RESPONSE_HEADER_WATTS], NULL) / 10.0f; obj->voltage = strtod (tokens[offset+DKP_WUP_RESPONSE_HEADER_VOLTS], NULL) / 10.0f; ret = TRUE; |