Don't simply trust that string splits were successful...

This commit is contained in:
Hisham Muhammad 2011-08-29 20:45:29 +00:00
parent 5dfb46e14f
commit 27b470e10d
4 changed files with 26 additions and 14 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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;
}

View File

@ -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);