Skip to content

Commit

Permalink
Add check around atoi() conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Arnaud Quette <[email protected]>
  • Loading branch information
aquette committed Feb 26, 2019
1 parent 155e795 commit 9a40084
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion common/snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#include "config.h"

#include <string.h>
# include <ctype.h>
#include <ctype.h>
#include <sys/types.h>

#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF)
Expand Down
41 changes: 33 additions & 8 deletions server/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "sstate.h"
#include "user.h"
#include "netssl.h"
#include <ctype.h>

ups_t *upstable = NULL;
int num_ups = 0;
Expand Down Expand Up @@ -121,20 +122,38 @@ static int parse_upsd_conf_args(int numargs, char **arg)

/* MAXAGE <seconds> */
if (!strcmp(arg[0], "MAXAGE")) {
maxage = atoi(arg[1]);
return 1;
if (isdigit(arg[1])) {

This comment has been minimized.

Copy link
@clepple

clepple Mar 2, 2019

Member

@aquette isdigit() takes a char/int rather than a char *. search for server/conf.c in http://buildbot.networkupstools.org/public/nut/builders/FreeBSD-x64/builds/593/steps/compile/logs/stdio

This comment has been minimized.

Copy link
@zykh

zykh Mar 2, 2019

Contributor

(@clepple, sorry for 'abusing' your comment)

unrelated, but one of the reasons behind the str API (improved a bit in the libusb-compat-1.0 branch) was our 'wild' use of string-to-number conversions: why not taking advantage of it?

Something like the following seems simpler to me (everywhere, not just for MAXAGE):

(after making maxage, and friends, unsigned int in upsd.{c,h}, and certrequest in netssl.{c,h}, since we should not expect/accept negative and/or signed numbers)

unsigned int	temp;

	...

/* MAXAGE <seconds> */
if (!strcmp(arg[0], "MAXAGE")) {
	if (str_to_uint(arg[1], &temp, 10)) {
		maxage = temp;
		return 1;
	}
	upslogx(LOG_ERR, "Could not convert MAXAGE (%s) to an unsigned int (%s)!", arg[1], strerror(errno));
	return 0;
}

(or str_to_uint_strict(), if we don't want to allow spaces)

This comment has been minimized.

Copy link
@aquette

aquette Mar 5, 2019

Author Member

wow, quite a pity that I completely forgot that we have that str API!
fully agreeing with @zykh comments (along with yours too @clepple).
and also wondering if retirement time has come for me :D

maxage = atoi(arg[1]);
return 1;
}
else {
upslogx(LOG_ERR, "MAXAGE has non numeric value (%s)!", arg[1]);
return 0;
}
}

/* TRACKINGDELAY <seconds> */
if (!strcmp(arg[0], "TRACKINGDELAY")) {
tracking_delay = atoi(arg[1]);
return 1;
if (isdigit(arg[1])) {
tracking_delay = atoi(arg[1]);
return 1;
}
else {
upslogx(LOG_ERR, "TRACKINGDELAY has non numeric value (%s)!", arg[1]);
return 0;
}
}

/* MAXCONN <connections> */
if (!strcmp(arg[0], "MAXCONN")) {
maxconn = atoi(arg[1]);
return 1;
if (isdigit(arg[1])) {
maxconn = atoi(arg[1]);
return 1;
}
else {
upslogx(LOG_ERR, "MAXCONN has non numeric value (%s)!", arg[1]);
return 0;
}
}

/* STATEPATH <dir> */
Expand Down Expand Up @@ -168,8 +187,14 @@ static int parse_upsd_conf_args(int numargs, char **arg)
#ifdef WITH_CLIENT_CERTIFICATE_VALIDATION
/* CERTREQUEST (0 | 1 | 2) */
if (!strcmp(arg[0], "CERTREQUEST")) {
certrequest = atoi(arg[1]);
return 1;
if (isdigit(arg[1])) {
certrequest = atoi(arg[1]);
return 1;
}
else {
upslogx(LOG_ERR, "CERTREQUEST has non numeric value (%s)!", arg[1]);
return 0;
}
}
#endif /* WITH_CLIENT_CERTIFICATE_VALIDATION */
#endif /* WITH_OPENSSL | WITH_NSS */
Expand Down

0 comments on commit 9a40084

Please sign in to comment.