summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-03-15 16:17:49 +0000
committerThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-03-15 16:17:49 +0000
commitffab7ee68b32d44ae2f35f688f417cd0109b0b45 (patch)
tree664a4d469f3772e5c91d483262fe384f2f7f6197 /lib
parent6004d95868fcd9badbc0f026980a4a3597eb22db (diff)
downloadmonitoring-plugins-ffab7ee68b32d44ae2f35f688f417cd0109b0b45.tar.gz
Fix segfault in test_ini.c and uncomment the affected tests
Fix bug in stanza parsing where full section could be skipped Fix single-argument options git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1946 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib')
-rw-r--r--lib/parse_ini.c25
-rw-r--r--lib/tests/test_ini.c26
2 files changed, 27 insertions, 24 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index c915d795..b555316e 100644
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
@@ -85,14 +85,14 @@ np_arg_list* np_get_defaults(const char *locator, const char *default_section){
85 /* if a file was specified or if we're using the default file */ 85 /* if a file was specified or if we're using the default file */
86 if(i.file != NULL && strlen(i.file) > 0){ 86 if(i.file != NULL && strlen(i.file) > 0){
87 if(strcmp(i.file, "-")==0){ 87 if(strcmp(i.file, "-")==0){
88 inifile=stdout; 88 inifile=stdout; /* FIXME: Shouldn't it be 'stdin' ??? */
89 } else { 89 } else {
90 inifile=fopen(i.file, "r"); 90 inifile=fopen(i.file, "r");
91 } 91 }
92 if(inifile==NULL) die(STATE_UNKNOWN, _("Config file error")); 92 if(inifile==NULL) die(STATE_UNKNOWN, _("Config file error"));
93 defaults=read_defaults(inifile, i.stanza); 93 defaults=read_defaults(inifile, i.stanza);
94 free(i.file); 94 free(i.file);
95 if(inifile!=stdout) fclose(inifile); 95 if(inifile!=stdout) fclose(inifile); /* FIXME: Shouldn't it be 'stdin' ??? */
96 } 96 }
97 free(i.stanza); 97 free(i.stanza);
98 return defaults; 98 return defaults;
@@ -126,9 +126,9 @@ static np_arg_list* read_defaults(FILE *f, const char *stanza){
126 stanzastate=WRONGSTANZA; 126 stanzastate=WRONGSTANZA;
127 for(i=0; i<stanza_len; i++){ 127 for(i=0; i<stanza_len; i++){
128 c=fgetc(f); 128 c=fgetc(f);
129 /* nope, read to the end of the stanza header */ 129 /* nope, read to the end of the line */
130 if(c!=stanza[i]) { 130 if(c!=stanza[i]) {
131 GOBBLE_TO(f, c, ']'); 131 GOBBLE_TO(f, c, '\n');
132 break; 132 break;
133 } 133 }
134 } 134 }
@@ -215,14 +215,18 @@ static int add_option(FILE *f, np_arg_list **optlst){
215 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++); 215 for(valptr=eqptr+1; valptr<lineend && isspace(*valptr); valptr++);
216 /* continue to the end of value (FIXME: watching for trailing comments) */ 216 /* continue to the end of value (FIXME: watching for trailing comments) */
217 for(valend=valptr; valend<lineend; valend++) 217 for(valend=valptr; valend<lineend; valend++)
218 /* FIXME: N::P doesn't allow comments. Remove next line and parse_ini won't either */ 218 /* FIXME: N::P doesn't allow comments here. Remove next line and parse_ini won't either */
219 if(*valend=='#') break; 219 if(*valend=='#') break;
220 --valend; 220 --valend;
221 /* Finally trim off trailing spaces */ 221 /* Finally trim off trailing spaces */
222 for(valend; isspace(*valend); valend--); 222 for(valend; isspace(*valend); valend--);
223 /* calculate the length of "--foo" */ 223 /* calculate the length of "--foo" */
224 opt_len=1+optend-optptr; 224 opt_len=1+optend-optptr;
225 cfg_len=2+(opt_len); 225 /* 1-character params needs only one dash */
226 if (opt_len==1)
227 cfg_len=1+(opt_len);
228 else
229 cfg_len=2+(opt_len);
226 /* if valptr<lineend then we have to also allocate space for "=bar" */ 230 /* if valptr<lineend then we have to also allocate space for "=bar" */
227 if(valptr<lineend) { 231 if(valptr<lineend) {
228 equals=value=1; 232 equals=value=1;
@@ -243,7 +247,14 @@ static int add_option(FILE *f, np_arg_list **optlst){
243 247
244 read_pos=0; 248 read_pos=0;
245 optnew->arg=(char *)malloc(cfg_len+1); 249 optnew->arg=(char *)malloc(cfg_len+1);
246 strncpy(&optnew->arg[read_pos], "--", 2); read_pos+=2; 250 /* 1-character params needs only one dash */
251 if (opt_len==1) {
252 strncpy(&optnew->arg[read_pos], "-", 1);
253 read_pos+=1;
254 } else {
255 strncpy(&optnew->arg[read_pos], "--", 2);
256 read_pos+=2;
257 }
247 strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len; 258 strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
248 if(equals) optnew->arg[read_pos++]='='; 259 if(equals) optnew->arg[read_pos++]='=';
249 if(value) { 260 if(value) {
diff --git a/lib/tests/test_ini.c b/lib/tests/test_ini.c
index b02d1452..de9f8adf 100644
--- a/lib/tests/test_ini.c
+++ b/lib/tests/test_ini.c
@@ -36,12 +36,13 @@ list2str(np_arg_list *optlst)
36 char *optstr=NULL; 36 char *optstr=NULL;
37 37
38 /* Put everything as a space-separated string */ 38 /* Put everything as a space-separated string */
39 asprintf(&optstr, "");
39 while (optlst) { 40 while (optlst) {
40 asprintf(&optstr, "%s%s ", optstr?optstr:"", optlst->arg); 41 asprintf(&optstr, "%s%s ", optstr, optlst->arg);
41 optlst=optlst->next; 42 optlst=optlst->next;
42 } 43 }
43 /* Strip last whitespace */ 44 /* Strip last whitespace */
44 optstr[strlen(optstr)-1]='\0'; 45 if (strlen(optstr)>1) optstr[strlen(optstr)-1]='\0';
45 46
46 return optstr; 47 return optstr;
47} 48}
@@ -51,7 +52,7 @@ main (int argc, char **argv)
51{ 52{
52 char *optstr=NULL; 53 char *optstr=NULL;
53 54
54 plan_tests(4); 55 plan_tests(9);
55 56
56 optstr=list2str(np_get_defaults("section@./config-tiny.ini", "check_disk")); 57 optstr=list2str(np_get_defaults("section@./config-tiny.ini", "check_disk"));
57 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank="), "config-tiny.ini's section as expected"); 58 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank="), "config-tiny.ini's section as expected");
@@ -61,42 +62,33 @@ main (int argc, char **argv)
61 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank="), "Used default section name, without specific"); 62 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank="), "Used default section name, without specific");
62 my_free(optstr); 63 my_free(optstr);
63 64
64 /* This test currently crashes */ 65 optstr=list2str(np_get_defaults("section_unknown@./config-tiny.ini", "section"));
65 /*
66 optstr=np_get_defaults("section_unknown@./config-tiny.ini", "section");
67 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank="), "Used default section name over specified one"); 66 ok( !strcmp(optstr, "--one=two --Foo=Bar --this=Your Mother! --blank="), "Used default section name over specified one");
68 my_free(optstr); 67 my_free(optstr);
69 */
70 68
71 optstr=list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk")); 69 optstr=list2str(np_get_defaults("Section Two@./config-tiny.ini", "check_disk"));
72 ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-tiny.ini's Section Two as expected"); 70 ok( !strcmp(optstr, "--something else=blah --remove=whitespace"), "config-tiny.ini's Section Two as expected");
73 my_free(optstr); 71 my_free(optstr);
74 72
75 /* These tests currently crash parse_ini.c */ 73 optstr=list2str(np_get_defaults("/path/to/file.txt@./config-tiny.ini", "check_disk"));
76 /*
77 optstr=np_get_defaults("/path/to/file.txt@./config-tiny.ini", "check_disk");
78 ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's filename as section name"); 74 ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's filename as section name");
79 my_free(optstr); 75 my_free(optstr);
80 76
81 optstr=np_get_defaults("section2@./config-tiny.ini", "check_disk"); 77 optstr=list2str(np_get_defaults("section2@./config-tiny.ini", "check_disk"));
82 ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section2 with whitespace before section name"); 78 ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section2 with whitespace before section name");
83 my_free(optstr); 79 my_free(optstr);
84 80
85 optstr=np_get_defaults("section3@./config-tiny.ini", "check_disk"); 81 optstr=list2str(np_get_defaults("section3@./config-tiny.ini", "check_disk"));
86 ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section3 with whitespace after section name"); 82 ok( !strcmp(optstr, "--this=that"), "config-tiny.ini's section3 with whitespace after section name");
87 my_free(optstr); 83 my_free(optstr);
88 */
89 84
90 optstr=list2str(np_get_defaults("check_mysql@./plugin.ini", "check_disk")); 85 optstr=list2str(np_get_defaults("check_mysql@./plugin.ini", "check_disk"));
91 ok( !strcmp(optstr, "--username=operator --password=secret"), "plugin.ini's check_mysql as expected"); 86 ok( !strcmp(optstr, "--username=operator --password=secret"), "plugin.ini's check_mysql as expected");
92 my_free(optstr); 87 my_free(optstr);
93 88
94 /* This test crashes at the moment. I think it is not expecting single character parameter names */ 89 optstr=list2str(np_get_defaults("check_mysql2@./plugin.ini", "check_disk"));
95 /*
96 optstr=np_get_defaults("check_mysql2@./config-tiny.ini", "check_disk");
97 ok( !strcmp(optstr, "-u=admin -p=secret"), "plugin.ini's check_mysql2 as expected"); 90 ok( !strcmp(optstr, "-u=admin -p=secret"), "plugin.ini's check_mysql2 as expected");
98 my_free(optstr); 91 my_free(optstr);
99 */
100 92
101 return exit_status(); 93 return exit_status();
102} 94}