summaryrefslogtreecommitdiffstats
path: root/gatchat/gatresult.c
diff options
context:
space:
mode:
authorDenis Kenzior <denkenz@gmail.com>2009-06-15 17:41:50 -0500
committerDenis Kenzior <denkenz@gmail.com>2009-06-16 16:38:24 -0500
commitb648a112f2d2a01b2f7adb7b847ce1731601952d (patch)
tree0b145b3538373de109a17cefd9d55b3549b5c72e /gatchat/gatresult.c
parent69372e642ed063d15dec6662984b7699640a4e11 (diff)
downloadofono-b648a112f2d2a01b2f7adb7b847ce1731601952d.tar.bz2
Improve string parsing code
Currently next_string and next_hexstring functions use a static buffer in the iterator to store the value. This value is clobbered as soon as next_string or next_hexstring is called. Instead, we copy the entire line in iter_next and use it as a scratch buffer. The only limitation is that lines of max 2048 are possible, however these are limited to around this size by parts of the standard.
Diffstat (limited to 'gatchat/gatresult.c')
-rw-r--r--gatchat/gatresult.c33
1 files changed, 18 insertions, 15 deletions
diff --git a/gatchat/gatresult.c b/gatchat/gatresult.c
index 8da88211..7a983083 100644
--- a/gatchat/gatresult.c
+++ b/gatchat/gatresult.c
@@ -43,13 +43,18 @@ gboolean g_at_result_iter_next(GAtResultIter *iter, const char *prefix)
{
char *line;
int prefix_len = prefix ? strlen(prefix) : 0;
+ int linelen;
while ((iter->l = iter->l->next)) {
line = iter->l->data;
+ linelen = strlen(line);
+
+ if (linelen > G_AT_RESULT_LINE_LENGTH_MAX)
+ continue;
if (prefix_len == 0) {
iter->line_pos = 0;
- return TRUE;
+ goto out;
}
if (g_str_has_prefix(line, prefix) == FALSE)
@@ -61,10 +66,15 @@ gboolean g_at_result_iter_next(GAtResultIter *iter, const char *prefix)
line[iter->line_pos] == ' ')
iter->line_pos += 1;
- return TRUE;
+ goto out;
}
return FALSE;
+
+out:
+ /* Already checked the length to be no more than buflen */
+ strcpy(iter->buf, line);
+ return TRUE;
}
const char *g_at_result_iter_raw_line(GAtResultIter *iter)
@@ -116,7 +126,7 @@ gboolean g_at_result_iter_next_string(GAtResultIter *iter, const char **str)
/* Omitted string */
if (line[pos] == ',') {
end = pos;
- memset(iter->buf, 0, sizeof(iter->buf));
+ iter->buf[pos] = '\0';
goto out;
}
@@ -131,11 +141,7 @@ gboolean g_at_result_iter_next_string(GAtResultIter *iter, const char **str)
if (line[end] != '"')
return FALSE;
- if (end - pos >= sizeof(iter->buf))
- return FALSE;
-
- strncpy(iter->buf, line+pos, end-pos);
- memset(iter->buf + end - pos, 0, sizeof(iter->buf) - end + pos);
+ iter->buf[end] = '\0';
/* Skip " */
end += 1;
@@ -144,7 +150,7 @@ out:
iter->line_pos = skip_to_next_field(line, end, len);
if (str)
- *str = iter->buf;
+ *str = iter->buf + pos;
return TRUE;
}
@@ -172,7 +178,7 @@ gboolean g_at_result_iter_next_hexstring(GAtResultIter *iter,
/* Omitted string */
if (line[pos] == ',') {
end = pos;
- memset(iter->buf, 0, sizeof(iter->buf));
+ iter->buf[pos] = '\0';
goto out;
}
@@ -184,19 +190,16 @@ gboolean g_at_result_iter_next_hexstring(GAtResultIter *iter,
if ((end - pos) & 1)
return FALSE;
- if ((end - pos) / 2 >= sizeof(iter->buf))
- return FALSE;
*length = (end - pos) / 2;
- for (bufpos = iter->buf; pos < end; pos += 2)
+ for (bufpos = iter->buf + pos; pos < end; pos += 2)
sscanf(line + pos, "%02hhx", bufpos++);
- memset(bufpos, 0, sizeof(iter->buf) - (bufpos - iter->buf));
out:
iter->line_pos = skip_to_next_field(line, end, len);
if (str)
- *str = (guint8 *) iter->buf;
+ *str = (guint8 *) bufpos - *length;
return TRUE;
}