Skip to content

Commit

Permalink
Use malloc() for members
Browse files Browse the repository at this point in the history
  • Loading branch information
andy5995 committed Feb 21, 2024
1 parent 602841f commit 6fdd2bd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
62 changes: 42 additions & 20 deletions canfigger.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This file is part of canfigger<https://github.com/andy5995/canfigger>
Copyright (C) 2021-2022 Andy Alt ([email protected])
Copyright (C) 2021-2024 Andy Alt ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -32,6 +32,8 @@ canfigger_free(st_canfigger_node * node)
if (node)
{
canfigger_free(node->next);
free(node->key);
free(node->value);
free(node);
}
return;
Expand All @@ -44,6 +46,8 @@ canfigger_free_attr(st_canfigger_attr_node * node)
if (node)
{
canfigger_free_attr(node->next);
if (node->str)
free(node->str);
free(node);
}
return;
Expand Down Expand Up @@ -109,24 +113,28 @@ trim_whitespace(char *str)


static char *
grab_str_segment(char *a, char *dest, const int c)
grab_str_segment(char *a, char **dest, const int c)
{
a = erase_lead_char(' ', a);
trim_whitespace(a);
char *b = strchr(a, c);
if (!b)
{
strcpy(dest, a);
trim_whitespace(dest);
*dest = strdup(a);
fprintf(stderr, "dest: '%s'\n", *dest);
return NULL;
}

char *dest_ptr = dest;
while (a != b)
*dest_ptr++ = *a++;

*dest_ptr = '\0';
trim_whitespace(dest);

size_t len = b - a;
*dest = strndup(a, len);
fprintf(stderr, "b-a = %d\n", len);

Check failure

Code scanning / CodeQL

Wrong type of arguments to formatting function High

This argument should be of type 'int' but is of type 'unsigned long'.
// fprintf(stderr, "delimiter: %c\n", c);
if (!*dest)
{
fprintf(stderr, "not_dest: '%s'\n", *dest);
return NULL;
}
trim_whitespace(*dest);
return b + 1;
}

Expand All @@ -141,9 +149,20 @@ canfigger_parse_file(const char *file, const int delimiter)
if (!fp)
return NULL;

char line[__CFG_LEN_MAX_LINE];
while (fgets(line, sizeof line, fp) != NULL)
char *line = NULL;
size_t len = 0;
ssize_t read;

// getline() malloc's the memory needed for line
while ((read = getline(&line, &len, fp)) != -1)
{
if (!line)
{
fclose(fp);
return NULL;
}

fprintf(stderr, "Retrieved line of length %zu:\n", read);
trim_whitespace(line);
char *a = line;

Expand All @@ -164,14 +183,13 @@ canfigger_parse_file(const char *file, const int delimiter)
st_canfigger_attr_node *attr_root = NULL;
st_canfigger_attr_node *attr_list = NULL;

*tmp_node->key = '\0';
*tmp_node->value = '\0';
char *b = grab_str_segment(a, &tmp_node->key, '=');

char *b = grab_str_segment(a, tmp_node->key, '=');
fprintf(stderr, "key: '%s'\n", tmp_node->key);
if (b)
{
a = b;
b = grab_str_segment(a, tmp_node->value, delimiter);
b = grab_str_segment(a, &tmp_node->value, delimiter);
}
do
{
Expand All @@ -185,6 +203,7 @@ canfigger_parse_file(const char *file, const int delimiter)
if (root)
canfigger_free(root);
fclose(fp);
free(line);
return NULL;
}

Expand All @@ -193,12 +212,11 @@ canfigger_parse_file(const char *file, const int delimiter)
else
attr_root = cur_attr_node;

*cur_attr_node->str = '\0';

cur_attr_node->str = "";
if (b)
{
a = b;
b = grab_str_segment(a, cur_attr_node->str, delimiter);
b = grab_str_segment(a, &cur_attr_node->str, delimiter);
}
attr_list = cur_attr_node;
cur_attr_node->next = NULL;
Expand All @@ -216,16 +234,20 @@ canfigger_parse_file(const char *file, const int delimiter)
canfigger_free(root);

fclose(fp);
free(line);
return NULL;
}
}

int r = fclose(fp);
if (r != 0)
{
if (line)
free(line);
return NULL;
}

free(line);
list = root;
return list;
}
12 changes: 4 additions & 8 deletions canfigger.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This file is part of canfigger<https://github.com/andy5995/canfigger>
Copyright (C) 2021-2022 Andy Alt ([email protected])
Copyright (C) 2021-2024 Andy Alt ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -23,16 +23,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define CANFIGGER_VERSION "0.2.0999"
#endif

// The max length of a line in a configuration file; a longer line will
// get truncated when fgets() is called to read the file.
#define __CFG_LEN_MAX_LINE (512 + 1)

// Member of st_canfigger_node
// @see canfigger_free_attr()
typedef struct st_canfigger_attr_node st_canfigger_attr_node;
struct st_canfigger_attr_node
{
char str[__CFG_LEN_MAX_LINE];
char *str;
st_canfigger_attr_node *next;
};

Expand All @@ -41,10 +37,10 @@ typedef struct st_canfigger_node st_canfigger_node;
struct st_canfigger_node
{
// Contains the string that precedes the '=' sign
char key[__CFG_LEN_MAX_LINE];
char *key;

// Contains the string between the '=' sign and the delimiter
char value[__CFG_LEN_MAX_LINE];
char *value;

// Linked list of attributes
st_canfigger_attr_node *attr_node;
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parse_file2.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ main(void)
snprintf(test_config_file, sizeof test_config_file,
"%s/no_exist_test_canfigger.conf",
SOURCE_DIR) < sizeof test_config_file);
st_canfigger_list *list = canfigger_parse_file(test_config_file, ',');

st_canfigger_list *list = canfigger_parse_file(test_config_file, ',');
assert(list == NULL);
assert(errno);
perror(__func__);
Expand Down

0 comments on commit 6fdd2bd

Please sign in to comment.