From 27b470e10d0ed2095e9e132193a6ae1ecff918bf Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 29 Aug 2011 20:45:29 +0000 Subject: [PATCH] Don't simply trust that string splits were successful... --- ProcessList.c | 10 +++++++--- Settings.c | 24 +++++++++++++++--------- String.c | 4 +++- String.h | 2 +- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/ProcessList.c b/ProcessList.c index aa5f8678..2a01d8e1 100644 --- a/ProcessList.c +++ b/ProcessList.c @@ -475,10 +475,14 @@ static void ProcessList_readCGroupFile(Process* process, const char* dirname, co char *ok = fgets(buffer, 255, file); if (ok) { char* trimmed = String_trim(buffer); - char** fields = String_split(trimmed, ':'); + int nFields; + char** fields = String_split(trimmed, ':', &nFields); free(trimmed); - - process->cgroup = strndup(fields[2] + 1, 10); + if (nFields >= 3) { + process->cgroup = strndup(fields[2] + 1, 10); + } else { + process->cgroup = strdup(""); + } String_freeArray(fields); } fclose(file); diff --git a/Settings.c b/Settings.c index ee9bafb6..3daf3c11 100644 --- a/Settings.c +++ b/Settings.c @@ -34,10 +34,10 @@ void Settings_delete(Settings* this) { static void Settings_readMeters(Settings* this, char* line, HeaderSide side) { char* trim = String_trim(line); - char** ids = String_split(trim, ' '); + int nIds; + char** ids = String_split(trim, ' ', &nIds); free(trim); - int i; - for (i = 0; ids[i] != NULL; i++) { + for (int i = 0; ids[i]; i++) { Header_createMeter(this->header, ids[i], side); } String_freeArray(ids); @@ -45,10 +45,10 @@ static void Settings_readMeters(Settings* this, char* line, HeaderSide side) { static void Settings_readMeterModes(Settings* this, char* line, HeaderSide side) { char* trim = String_trim(line); - char** ids = String_split(trim, ' '); + int nIds; + char** ids = String_split(trim, ' ', &nIds); free(trim); - int i; - for (i = 0; ids[i] != NULL; i++) { + for (int i = 0; ids[i]; i++) { int mode = atoi(ids[i]); Header_setMode(this->header, i, mode, side); } @@ -67,13 +67,19 @@ static bool Settings_read(Settings* this, char* fileName) { char buffer[maxLine]; bool readMeters = false; while (fgets(buffer, maxLine, fd)) { - char** option = String_split(buffer, '='); + int nOptions; + char** option = String_split(buffer, '=', &nOptions); + if (nOptions < 2) { + String_freeArray(option); + continue; + } if (String_eq(option[0], "fields")) { char* trim = String_trim(option[1]); - char** ids = String_split(trim, ' '); + int nIds; + char** ids = String_split(trim, ' ', &nIds); free(trim); int i, j; - for (j = 0, i = 0; i < LAST_PROCESSFIELD && ids[i] != NULL; i++) { + for (j = 0, i = 0; i < LAST_PROCESSFIELD && ids[i]; i++) { // This "+1" is for compatibility with the older enum format. int id = atoi(ids[i]) + 1; if (id > 0 && id < LAST_PROCESSFIELD) { diff --git a/String.c b/String.c index 2ee24ac0..81e9eeeb 100644 --- a/String.c +++ b/String.c @@ -55,7 +55,8 @@ inline int String_eq(const char* s1, const char* s2) { return (strcmp(s1, s2) == 0); } -char** String_split(const char* s, char sep) { +char** String_split(const char* s, char sep, int* n) { + *n = 0; const int rate = 10; char** out = (char**) malloc(sizeof(char*) * rate); int ctr = 0; @@ -83,6 +84,7 @@ char** String_split(const char* s, char sep) { } out = realloc(out, sizeof(char*) * (ctr + 1)); out[ctr] = NULL; + *n = ctr; return out; } diff --git a/String.h b/String.h index d89fe8c2..e8de7dcf 100644 --- a/String.h +++ b/String.h @@ -27,7 +27,7 @@ char* String_trim(const char* in); extern int String_eq(const char* s1, const char* s2); -char** String_split(const char* s, char sep); +char** String_split(const char* s, char sep, int* n); void String_freeArray(char** s);