Merge pull request #341 from dequis/strsplit-len-fix

Fix invalid reads in strsplit_len when splitting on spaces
This commit is contained in:
ailin-nemui 2015-11-09 16:21:33 +01:00
commit 0188c1fb5d

View File

@ -997,22 +997,22 @@ char **strsplit_len(const char *str, int len, gboolean onspace)
int n; int n;
int offset; int offset;
for (n = 0; *str != '\0'; n++, str += MIN(len - offset, strlen(str))) { for (n = 0; *str != '\0'; n++, str += offset) {
offset = 0; offset = MIN(len, strlen(str));
if (onspace) { if (onspace && strlen(str) > len) {
/* /*
* Try to find a space to split on and leave * Try to find a space to split on and leave
* the space on the previous line. * the space on the previous line.
*/ */
int i; int i;
for (i = 0; i < len; i++) { for (i = len - 1; i > 0; i--) {
if (str[len-1-i] == ' ') { if (str[i] == ' ') {
offset = i; offset = i;
break; break;
} }
} }
} }
ret[n] = g_strndup(str, len - offset); ret[n] = g_strndup(str, offset);
ret = g_renew(char *, ret, n + 2); ret = g_renew(char *, ret, n + 2);
} }
ret[n] = NULL; ret[n] = NULL;