summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@iohq.net>2015-02-25 12:01:07 -0700
committerAaron Ball <nullspoon@iohq.net>2015-02-25 12:01:07 -0700
commitd89446f3e2d495ff9b5ca6528ec48e7f4233bb90 (patch)
tree7c063fcc7edad785eacf61a49ddaec64d37a827c
parent83f7cbe3d162db0985c3e6daed068c0da73b5623 (diff)
downloadnoteless-d89446f3e2d495ff9b5ca6528ec48e7f4233bb90.tar.gz
noteless-d89446f3e2d495ff9b5ca6528ec48e7f4233bb90.tar.xz
Fixed various valgrind complaints
Fixed a few buffer overflow problems where enough space for the trailing \0 in a char array wasn't accounted for in the malloc statement. Also fixed some initialization problems with variable length arrays. Created the config_line_len global variable for this one so it can remain "variable" while being static (we don't have to hunt down 10 different instances of the value when we want to change it).
-rw-r--r--src/config.c15
-rw-r--r--src/config.h1
-rw-r--r--src/note_list.c2
3 files changed, 10 insertions, 8 deletions
diff --git a/src/config.c b/src/config.c
index a8c83d2..c3509cc 100644
--- a/src/config.c
+++ b/src/config.c
@@ -43,8 +43,8 @@ int config_new(config_t* conf, char* p) {
}
// Set the maximum read width and read buffer array
- int len = 120;
- char buf[len];
+ int len = config_line_len;
+ char buf[config_line_len] = "";
char key[32];
char value[256];
@@ -74,7 +74,8 @@ int config_new(config_t* conf, char* p) {
end++;
}
- char tmp_line[end - start + 10];
+ //char tmp_line[end - start + 10];
+ char tmp_line[config_line_len] = "";
// Temp variable to store the text between index start and index end
strncpy(tmp_line, &buf[start], end - start + 1);
// Make sure it has a trailing newline so sscanf works
@@ -86,8 +87,8 @@ int config_new(config_t* conf, char* p) {
// Allocate heap memory for values since these need to persist.
// Note that here we are using the temp variables to establish string
// length for when we allocate heap memory.
- conf->keys[i] = malloc(sizeof(char) * strlen(key));
- conf->values[i] = malloc(sizeof(char) * strlen(value));
+ conf->keys[i] = malloc(sizeof(char) * (strlen(key) + 1));
+ conf->values[i] = malloc(sizeof(char) * (strlen(value) + 1));
// Copy in the temp values
strcpy(conf->keys[i], key);
@@ -134,8 +135,8 @@ int config_valid_line_count(char* path) {
FILE* f;
f = fopen(path, "r");
- int len = 120;
- char buf[len];
+ int len = config_line_len;
+ char buf[config_line_len];
while(fgets(buf, len, f) != NULL) {
int start = 0;
diff --git a/src/config.h b/src/config.h
index 2a3e0d0..295969d 100644
--- a/src/config.h
+++ b/src/config.h
@@ -18,6 +18,7 @@
*/
#ifndef NOTELESS_CONFIG_H
#define NOTELESS_CONFIG_H
+#define config_line_len 120
#include <stdlib.h>
#include <stdio.h>
diff --git a/src/note_list.c b/src/note_list.c
index 79b9aa9..2566b59 100644
--- a/src/note_list.c
+++ b/src/note_list.c
@@ -58,7 +58,7 @@ void note_list_new(note_list_t* list, char* path, char* ext) {
// Allocate space for the entire filename (we'll strip the extension off
// as needed later). This will make calculating path lengths much
// simpler.
- list->names[i] = malloc(sizeof(char) * strlen(ent->d_name));
+ list->names[i] = malloc(sizeof(char) * (strlen(ent->d_name) + 1));
// Copy the filename into the struct
strcpy(list->names[i], ent->d_name);

Generated by cgit