Skip to content

Commit

Permalink
changes to fix segfault on too man ini lines
Browse files Browse the repository at this point in the history
- Makefile:
  define INI_MAX_LINES to 2000
  define INI_USE_STACK to 0
  add support for building executable test driver
- nvram-faker-internal.h
  changes to support linking into exe
  conditionally #define INI_FILE_PATH
  conditionally declare constructor & destructor if not building exe
- nvram-faker.c
  fix major crasher: allocate proper amount of memory for kv pairs
  move several #defines to nvram-faker-internal.h
  • Loading branch information
zcutlip committed Jan 31, 2015
1 parent 822ab7a commit 7a8d09b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
21 changes: 18 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
TRUNK ?=.
include $(TRUNK)/arch.mk

TEST=test-write-pid

AR?=ar

CFLAGS+=-g
CFLAGS+=-ggdb
CFLAGS+= -DINI_MAX_LINE=2000
CFLAGS+= -DINI_USE_STACK=0
export CFLAGS
EXE=nvram_faker_exe
EXE_OBJ=nvram_faker_main.o
OBJS=nvram-faker.o
INI_OBJ=ini.o
INI_PATH=$(TRUNK)/contrib/inih
Expand All @@ -16,19 +20,30 @@ LIB=libnvram-faker.so
all:$(LIB)



exe: export CFLAGS+=-DNVRAM_EXE -DDEBUG
exe: export CFLAGS+=-DINI_FILE_PATH=\"./nvram.ini\"
exe: $(EXE)



$(INI_OBJ):
make -C $(INI_PATH) $@
cp $(INI_PATH)/$@ .

%.o:%.c
$(CC) $(INCLUDES) $(CFLAGS) -fPIC -c -o $@ $<
$(CC) -Wall $(INCLUDES) $(CFLAGS) -fPIC -c -o $@ $<

$(LIB): $(OBJS) $(INI_OBJ)
$(CC) -shared -o $@ $^ -Wl,-nostdlib

nvram_faker_exe:$(EXE_OBJ) $(OBJS) $(INI_OBJ)
$(CC) -Wall -o $@ $^

clean:
-rm *.o
-rm *.so
-rm $(EXE)
make -C $(INI_PATH) $@


12 changes: 12 additions & 0 deletions nvram-faker-internal.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef __nvram_faker_internal_h
#define __nvram_faker_internal_h

#ifndef INI_FILE_PATH
#define INI_FILE_PATH "/nvram.ini"
#endif

#ifndef INI_MAX_LINE
#define INI_MAX_LINE 1024
#endif /* INI_MAX_LINE */
Expand All @@ -9,6 +13,14 @@
#define INI_USE_STACK 0
#endif /* INI_USE_STACK */

#ifndef NVRAM_EXE
void initialize_ini(void) __attribute__((constructor));
void end(void) __attribute__((destructor));
#else
void initialize_ini(void);
void end(void);
#endif

#ifdef DEBUG
#define DEBUG_PRINTF(format,...) fprintf(stderr,format,## __VA_ARGS__)
#else
Expand Down
11 changes: 2 additions & 9 deletions nvram-faker.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,10 @@
#define RED_OFF "\033[22;00m"
#define DEFAULT_KV_PAIR_LEN 1024

#ifndef INI_FILE_PATH
#define INI_FILE_PATH "/nvram.ini"
#endif

static int kv_count=0;
static int key_value_pair_len=DEFAULT_KV_PAIR_LEN;
static char **key_value_pairs=NULL;

void initialize_ini(void) __attribute__((constructor));
void end(void) __attribute__((destructor));

static int ini_handler(void *user, const char *section, const char *name,const char *value)
{

Expand All @@ -41,6 +34,7 @@ static int ini_handler(void *user, const char *section, const char *name,const c
return 0;
}

DEBUG_PRINTF("kv_count: %d, key_value_pair_len: %d\n", kv_count,key_value_pair_len);
if(kv_count >= key_value_pair_len)
{
old_kv_len=key_value_pair_len;
Expand Down Expand Up @@ -72,7 +66,7 @@ void initialize_ini(void)
DEBUG_PRINTF("Initializing.\n");
if (NULL == key_value_pairs)
{
key_value_pairs=malloc(key_value_pair_len);
key_value_pairs=malloc(key_value_pair_len * sizeof(char **));
}
if(NULL == key_value_pairs)
{
Expand All @@ -81,7 +75,6 @@ void initialize_ini(void)
}

ret = ini_parse(INI_FILE_PATH,ini_handler,(void *)&key_value_pairs);

if (0 != ret)
{
LOG_PRINTF("ret from ini_parse was: %d\n",ret);
Expand Down

0 comments on commit 7a8d09b

Please sign in to comment.