Skip to content

Commit b80bc0b

Browse files
committed
Merge pull request #163 from mhaas/coverity-fixes
Coverity fixes
2 parents 6cb7e59 + d944031 commit b80bc0b

File tree

10 files changed

+988
-978
lines changed

10 files changed

+988
-978
lines changed

src/commandline.c

Lines changed: 101 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
* Holds an argv that could be passed to exec*() if we restart ourselves
4141
*/
4242
/* Declare variable */
43-
extern char ** restartargv;
43+
extern char **restartargv;
4444
/* Define variable */
45-
char ** restartargv = NULL;
45+
char **restartargv = NULL;
4646

4747
static void usage(void);
4848

@@ -73,115 +73,117 @@ usage(void)
7373
fprintf(stdout, " -w <path> Wdctl socket path\n");
7474
fprintf(stdout, " -h Print usage\n");
7575
fprintf(stdout, " -v Print version information\n");
76-
fprintf(stdout, " -x pid Used internally by WiFiDog when re-starting itself *DO NOT ISSUE THIS SWITCH MANUAlLY*\n");
76+
fprintf(stdout,
77+
" -x pid Used internally by WiFiDog when re-starting itself *DO NOT ISSUE THIS SWITCH MANUAlLY*\n");
7778
fprintf(stdout, " -i <path> Internal socket path used when re-starting self\n");
7879
fprintf(stdout, "\n");
7980
}
8081

8182
/** Uses getopt() to parse the command line and set configuration values
8283
* also populates restartargv
8384
*/
84-
void parse_commandline(int argc, char **argv) {
85-
int c;
86-
int skiponrestart;
87-
int i;
85+
void
86+
parse_commandline(int argc, char **argv)
87+
{
88+
int c;
89+
int skiponrestart;
90+
int i;
8891

8992
s_config *config = config_get_config();
9093

91-
//MAGIC 3: Our own -x, the pid, and NULL :
92-
restartargv = safe_malloc((size_t) (argc + 3) * sizeof(char*));
93-
i=0;
94-
restartargv[i++] = safe_strdup(argv[0]);
94+
//MAGIC 3: Our own -x, the pid, and NULL :
95+
restartargv = safe_malloc((size_t) (argc + 3) * sizeof(char *));
96+
i = 0;
97+
restartargv[i++] = safe_strdup(argv[0]);
9598

9699
while (-1 != (c = getopt(argc, argv, "c:hfd:sw:vx:i:"))) {
97100

98-
skiponrestart = 0;
99-
100-
switch(c) {
101-
102-
case 'h':
103-
usage();
104-
exit(1);
105-
break;
106-
107-
case 'c':
108-
if (optarg) {
109-
strncpy(config->configfile, optarg, sizeof(config->configfile));
110-
}
111-
break;
112-
113-
case 'w':
114-
if (optarg) {
115-
free(config->wdctl_sock);
116-
config->wdctl_sock = safe_strdup(optarg);
117-
}
118-
break;
119-
120-
case 'f':
121-
skiponrestart = 1;
122-
config->daemon = 0;
123-
break;
124-
125-
case 'd':
126-
if (optarg) {
127-
config->debuglevel = atoi(optarg);
128-
}
129-
break;
130-
131-
case 's':
132-
config->log_syslog = 1;
133-
break;
134-
135-
case 'v':
136-
fprintf(stdout, "This is WiFiDog version " VERSION "\n");
137-
exit(1);
138-
break;
139-
140-
case 'x':
141-
skiponrestart = 1;
142-
if (optarg) {
143-
restart_orig_pid = atoi(optarg);
144-
}
145-
else {
146-
fprintf(stdout, "The expected PID to the -x switch was not supplied!");
147-
exit(1);
148-
}
149-
break;
150-
151-
case 'i':
152-
if (optarg) {
153-
free(config->internal_sock);
154-
config->internal_sock = safe_strdup(optarg);
155-
}
156-
break;
157-
158-
default:
159-
usage();
160-
exit(1);
161-
break;
162-
163-
}
164-
165-
if (!skiponrestart) {
166-
/* Add it to restartargv */
167-
safe_asprintf(&(restartargv[i++]), "-%c", c);
168-
if (optarg) {
169-
restartargv[i++] = safe_strdup(optarg);
170-
}
171-
}
172-
173-
}
174-
175-
/* Finally, we should add the -x, pid and NULL to restartargv
176-
* HOWEVER we cannot do it here, since this is called before we fork to background
177-
* so we'll leave this job to gateway.c after forking is completed
178-
* so that the correct PID is assigned
179-
*
180-
* We add 3 nulls, and the first 2 will be overridden later
181-
*/
182-
restartargv[i++] = NULL;
183-
restartargv[i++] = NULL;
184-
restartargv[i++] = NULL;
101+
skiponrestart = 0;
102+
103+
switch (c) {
104+
105+
case 'h':
106+
usage();
107+
exit(1);
108+
break;
109+
110+
case 'c':
111+
if (optarg) {
112+
free(config->configfile);
113+
config->configfile = safe_strdup(optarg);
114+
}
115+
break;
116+
117+
case 'w':
118+
if (optarg) {
119+
free(config->wdctl_sock);
120+
config->wdctl_sock = safe_strdup(optarg);
121+
}
122+
break;
123+
124+
case 'f':
125+
skiponrestart = 1;
126+
config->daemon = 0;
127+
break;
128+
129+
case 'd':
130+
if (optarg) {
131+
config->debuglevel = atoi(optarg);
132+
}
133+
break;
134+
135+
case 's':
136+
config->log_syslog = 1;
137+
break;
138+
139+
case 'v':
140+
fprintf(stdout, "This is WiFiDog version " VERSION "\n");
141+
exit(1);
142+
break;
143+
144+
case 'x':
145+
skiponrestart = 1;
146+
if (optarg) {
147+
restart_orig_pid = atoi(optarg);
148+
} else {
149+
fprintf(stdout, "The expected PID to the -x switch was not supplied!");
150+
exit(1);
151+
}
152+
break;
153+
154+
case 'i':
155+
if (optarg) {
156+
free(config->internal_sock);
157+
config->internal_sock = safe_strdup(optarg);
158+
}
159+
break;
160+
161+
default:
162+
usage();
163+
exit(1);
164+
break;
165+
166+
}
167+
168+
if (!skiponrestart) {
169+
/* Add it to restartargv */
170+
safe_asprintf(&(restartargv[i++]), "-%c", c);
171+
if (optarg) {
172+
restartargv[i++] = safe_strdup(optarg);
173+
}
174+
}
175+
176+
}
177+
178+
/* Finally, we should add the -x, pid and NULL to restartargv
179+
* HOWEVER we cannot do it here, since this is called before we fork to background
180+
* so we'll leave this job to gateway.c after forking is completed
181+
* so that the correct PID is assigned
182+
*
183+
* We add 3 nulls, and the first 2 will be overridden later
184+
*/
185+
restartargv[i++] = NULL;
186+
restartargv[i++] = NULL;
187+
restartargv[i++] = NULL;
185188

186189
}
187-

src/commandline.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
#define _COMMANDLINE_H_
2929

3030
/** @brief Parses the command line and set the config accordingly */
31-
void parse_commandline(int, char**);
31+
void parse_commandline(int, char **);
3232

33-
#endif /* _COMMANDLINE_H_ */
33+
#endif /* _COMMANDLINE_H_ */

src/conf.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void
170170
config_init(void)
171171
{
172172
debug(LOG_DEBUG, "Setting default config parameters");
173-
strncpy(config.configfile, DEFAULT_CONFIGFILE, sizeof(config.configfile));
173+
config.configfile = safe_strdup(DEFAULT_CONFIGFILE);
174174
config.htmlmsgfile = safe_strdup(DEFAULT_HTMLMSGFILE);
175175
config.debuglevel = DEFAULT_DEBUGLEVEL;
176176
config.httpdmaxconn = DEFAULT_HTTPDMAXCONN;
@@ -874,7 +874,9 @@ parse_trusted_mac_list(const char *ptr)
874874
p->mac = safe_strdup(mac);
875875
p->next = NULL;
876876
} else {
877-
debug(LOG_ERR, "MAC address [%s] already on trusted list. See option TrustedMACList in wifidog.conf file ", mac);
877+
debug(LOG_ERR,
878+
"MAC address [%s] already on trusted list. See option TrustedMACList in wifidog.conf file ",
879+
mac);
878880
}
879881
}
880882
}

src/conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ typedef struct _trusted_mac_t {
147147
* Configuration structure
148148
*/
149149
typedef struct {
150-
char configfile[255]; /**< @brief name of the config file */
150+
char *configfile; /**< @brief name of the config file */
151151
char *htmlmsgfile; /**< @brief name of the HTML file used for messages */
152152
char *wdctl_sock; /**< @brief wdctl path to socket */
153153
char *internal_sock; /**< @brief internal path to socket */

0 commit comments

Comments
 (0)