commit d045dad66c4f1fcbec66576aa8b187eba04c79a2
parent 5daa6f38528cbbb18440014965a5874ea979145f
Author: Mohamed Aslan <maslan@sce.carleton.ca>
Date: Fri, 27 Jun 2014 08:38:30 -0400
fix a bug in config parser, where it incorrectly fails when username has whitespaces
Diffstat:
2 files changed, 35 insertions(+), 16 deletions(-)
diff --git a/config.c b/config.c
@@ -43,6 +43,16 @@ static struct option options[] = {
{.key = "editor", .val = ""}
};
+static char*
+skip_spaces(char *str)
+{
+ if (str == NULL)
+ return NULL;
+ while ((*str == ' ' || *str == '\t') && *str != '\0')
+ str++;
+ return str;
+}
+
int
baseline_config_create(const char *path)
{
@@ -56,13 +66,14 @@ baseline_config_create(const char *path)
return EXIT_SUCCESS;
}
+
int
baseline_config_load(const char *path)
{
char *line = NULL; /* must be initialized to NULL or getline()'s realloc() might complain */
char *ptr, *tmp;
char key[32], val[32];
- int i, idx = 0;
+ int i;
size_t size = 0;
ssize_t len;
FILE *fp;
@@ -74,23 +85,31 @@ baseline_config_load(const char *path)
/* skip blank lines */
if (line[0] == '\n')
continue;
- i = 0;
- /* skip white spaces */
- while(line[i] == ' ' || line[i] == '\t')
- i++;
- ptr = &line[i];
+ ptr = skip_spaces(line);
/* skip comments */
if (ptr[0] == '#' || ptr[0] == ';')
continue;
- if ((tmp = strchr(ptr, '=')) == NULL)
- return EXIT_FAILURE;
- *tmp = ' ';
- sscanf(ptr, "%s%s", key, val);
- if (!strcmp(options[idx].key, key)) {
- strlcpy(options[idx].val, val, sizeof(options[idx].val));
- idx++;
- if (idx > N_OPTIONS)
+ /* find the end of the key */
+ tmp = ptr;
+ while(*tmp != ' ' && *tmp != '\t'&& *tmp != '=')
+ tmp++;
+ *tmp++ = '\0';
+ strlcpy(key, ptr, sizeof(key));
+ tmp = skip_spaces(tmp);
+ /* skip the '=' */
+ if (*tmp == '=')
+ tmp++;
+ /* skip white spaces */
+ ptr = tmp = skip_spaces(tmp);
+ while (*tmp != '\n' && *tmp != '\0')
+ tmp++;
+ *tmp = '\0';
+ strlcpy(val, ptr, sizeof(val));
+ for (i = 0; i < N_OPTIONS ; i++) {
+ if (strcmp(options[i].key, key) == 0) {
+ strlcpy(options[i].val, val, sizeof(options[i].val));
break;
+ }
}
}
fclose(fp);
diff --git a/objdb-fs.c b/objdb-fs.c
@@ -226,7 +226,7 @@ commit_deserialize(int fd, struct commit *comm)
/* skip "author " */
p1 = buf + 7;
/* find author's name */
- if ((p2 = strchr(p1, ' ')) == NULL)
+ if ((p2 = strstr(p1, " <")) == NULL)
goto parse_error;
*p2 = '\0';
comm->author.name = strdup(p1);
@@ -257,7 +257,7 @@ commit_deserialize(int fd, struct commit *comm)
/* skip "committer " */
p1 = buf + 10;
/* find committer's name */
- if ((p2 = strchr(p1, ' ')) == NULL)
+ if ((p2 = strstr(p1, " <")) == NULL)
goto parse_error;
*p2 = '\0';
comm->committer.name = strdup(p1);