Skip to content

Commit

Permalink
Fix issue with deep file recursion (#14)
Browse files Browse the repository at this point in the history
* Fix stack overflow issue.

Caused by >=10 dash-f file depth.

Add test to cover large sknob strings and dash-f recursion.

* Make MAX a multiple of CHUNK.

This simplifies the logic.

Co-authored-by: Josh Scheid <[email protected]>
  • Loading branch information
joshuascheid and jscheid-ventana authored Jan 10, 2022
1 parent b2fc0ea commit 9cf65ed
Show file tree
Hide file tree
Showing 20 changed files with 3,056 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ INSTALL_LIB=lib64
default: libsknobs.a libsknobs.so

test: testsknobs
./testsknobs
./testsknobs -f test000.knobs

sknobs: sknobs.o

Expand Down
24 changes: 18 additions & 6 deletions src/c/sknobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

////////////////////////////////////////////////////////////////////////////
// defines
#define MAX_ARGV_SIZE 100000
#define ARGV_SIZE_CHUNK 1000
#define MAX_ARGV_SIZE (ARGV_SIZE_CHUNK * 100)
#define MAX_FILENAME_SIZE PATH_MAX
#define MAX_CHOICES 100
#define HASH_TABLE_SIZE 1031
Expand Down Expand Up @@ -921,21 +922,32 @@ int sknobs_load(int argc, char *argv[], char *comment) {
// return 1 on error
int sknobs_load_string(char *name, char *s, char *comment) {
// tokenize
int return_value = 0;
int argc = 0;
char *argv[MAX_ARGV_SIZE];
size_t current_argv_size;
char **argv;
char *token;
check_sknobs_init();
if (debug > 2) printf("sknobs_load_string: %s\n", s);
argv = malloc(ARGV_SIZE_CHUNK * sizeof(char*));
assert(argv);
current_argv_size = ARGV_SIZE_CHUNK;
argv[argc++] = name;
token = strtok(s, sknobs_delimiters);
while (token) {
assert(argc < MAX_ARGV_SIZE);
if(argc >= current_argv_size) {
// Incrementally add chunks.
assert(argc < MAX_ARGV_SIZE);
current_argv_size += ARGV_SIZE_CHUNK;
argv = realloc(argv, current_argv_size * sizeof(char*));
assert(argv);
}
argv[argc++] = token;
token = strtok(NULL, sknobs_delimiters);
}
if (sknobs_load(argc, argv, comment))
return 1;
return 0;
return_value = sknobs_load(argc, argv, comment);
free(argv);
return return_value;
}

////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 2 additions & 0 deletions src/c/test000.knobs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
+fromfile=0
-f test001.knobs
Loading

0 comments on commit 9cf65ed

Please sign in to comment.