aboutsummaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2019-12-29 01:35:09 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2019-12-29 01:35:09 -0500
commit4595ada2f69b42670c85a63c7d2344af63f2afe7 (patch)
tree0d528d8177aceefcf74fb7306fc0fc7cc3c41ece /src/config.c
parent8ae4f8f5985445b1ce3547975f407847c0fee0f7 (diff)
downloadspmc-4595ada2f69b42670c85a63c7d2344af63f2afe7.tar.gz
Minor fixes:
* size_t in place of int * Moved some variables closer to their execution scope * Add some error checks
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c30
1 files changed, 27 insertions, 3 deletions
diff --git a/src/config.c b/src/config.c
index 7b62311..9766e9b 100644
--- a/src/config.c
+++ b/src/config.c
@@ -24,14 +24,27 @@
ConfigItem **config_read(const char *filename) {
const char sep = '=';
char *line = (char *)calloc(CONFIG_BUFFER_SIZE, sizeof(char));
+ if (!line) {
+ perror("config line buffer");
+ fprintf(SYSERROR);
+ return NULL;
+ }
FILE *fp = fopen(filename, "r");
if (!fp) {
// errno will be set, so die, and let the caller handle it
+ free(line);
return NULL;
}
- int record_initial = 2;
+ size_t record_initial = 2;
+ size_t record = 0;
ConfigItem **config = (ConfigItem **) calloc(record_initial, sizeof(ConfigItem *));
- int record = 0;
+ if (!config) {
+ perror("ConfigItem");
+ fprintf(SYSERROR);
+ free(line);
+ fclose(fp);
+ return NULL;
+ }
while (fgets(line, CONFIG_BUFFER_SIZE, fp) != NULL) {
char *lptr = line;
@@ -53,7 +66,7 @@ ConfigItem **config_read(const char *filename) {
// Get a pointer to the key pair separator
char *sep_pos = strchr(lptr, sep);
if (!sep_pos) {
- printf("invalid entry on line %d: missing '%c': '%s'\n", record, sep, lptr);
+ printf("invalid entry on line %zu: missing '%c': '%s'\n", record, sep, lptr);
continue;
}
@@ -68,6 +81,12 @@ ConfigItem **config_read(const char *filename) {
config[record]->key = (char *)calloc(key_length + 1, sizeof(char));
config[record]->value = (char *)calloc(value_length + 1, sizeof(char));
+ if (!config[record] || !config[record]->key || !config[record]->value) {
+ perror("ConfigItem record");
+ fprintf(SYSERROR);
+ return NULL;
+ }
+
// Shortcut our array at this point. Things get pretty ugly otherwise.
char *key = config[record]->key;
char *value = config[record]->value;
@@ -117,6 +136,11 @@ ConfigItem **config_read(const char *filename) {
record++;
// Expand config by another record
config = (ConfigItem **)reallocarray(config, record + record_initial + 1, sizeof(ConfigItem *));
+ if (!config) {
+ perror("ConfigItem array");
+ fprintf(SYSERROR);
+ return NULL;
+ }
}
free(line);
return config;