Replace calls to strtoul() with a helper m_str_to_uint()

--HG--
extra : convert_revision : 1f8643c5ba7fe789c120b503c396281ac45f9730
This commit is contained in:
Matt Johnston 2008-09-22 14:13:44 +00:00
parent e1a8bf0240
commit 643626d546
5 changed files with 25 additions and 17 deletions

View File

@ -355,8 +355,7 @@ void cli_getopts(int argc, char ** argv) {
} }
} }
if (keepalive_arg) { if (keepalive_arg) {
opts.keepalive_secs = strtoul(keepalive_arg, NULL, 10); if (m_str_to_uint(keepalive_arg, &opts.keepalive_secs) == DROPBEAR_FAILURE) {
if (opts.keepalive_secs == 0 && errno == EINVAL) {
dropbear_exit("Bad keepalive '%s'", keepalive_arg); dropbear_exit("Bad keepalive '%s'", keepalive_arg);
} }
} }
@ -499,8 +498,7 @@ static void add_netcat(const char* origstr) {
goto fail; goto fail;
} }
cli_opts.netcat_port = strtoul(portstr, NULL, 10); if (m_str_to_uint(portstr, &cli_opts.netcat_port) == DROPBEAR_FAILURE) {
if (errno != 0) {
TRACE(("bad netcat port")) TRACE(("bad netcat port"))
goto fail; goto fail;
} }
@ -571,15 +569,13 @@ static void addforward(const char* origstr, struct TCPFwdList** fwdlist) {
/* Now we check the ports - note that the port ints are unsigned, /* Now we check the ports - note that the port ints are unsigned,
* the check later only checks for >= MAX_PORT */ * the check later only checks for >= MAX_PORT */
newfwd->listenport = strtoul(listenport, NULL, 10); if (m_str_to_uint(listenport, &newfwd->listenport) == DROPBEAR_FAILURE) {
if (errno != 0) { TRACE(("bad listenport strtoul"))
TRACE(("bad listenport strtol"))
goto fail; goto fail;
} }
newfwd->connectport = strtoul(connectport, NULL, 10); if (m_str_to_uint(connectport, &newfwd->connectport) == DROPBEAR_FAILURE) {
if (errno != 0) { TRACE(("bad connectport strtoul"))
TRACE(("bad connectport strtol"))
goto fail; goto fail;
} }

View File

@ -504,7 +504,7 @@ void run_shell_command(const char* cmd, unsigned int maxfd, char* usershell) {
if (cmd != NULL) { if (cmd != NULL) {
argv[1] = "-c"; argv[1] = "-c";
argv[2] = cmd; argv[2] = (char*)cmd;
argv[3] = NULL; argv[3] = NULL;
} else { } else {
/* construct a shell of the form "-bash" etc */ /* construct a shell of the form "-bash" etc */
@ -835,3 +835,17 @@ void disallow_core() {
lim.rlim_cur = lim.rlim_max = 0; lim.rlim_cur = lim.rlim_max = 0;
setrlimit(RLIMIT_CORE, &lim); setrlimit(RLIMIT_CORE, &lim);
} }
/* Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE, with the result in *val */
int m_str_to_uint(const char* str, unsigned int *val) {
errno = 0;
*val = strtoul(str, NULL, 10);
/* The c99 spec doesn't actually seem to define EINVAL, but most platforms
* I've looked at mention it in their manpage */
if ((*val == 0 && errno == EINVAL)
|| (*val == ULONG_MAX && errno == ERANGE)) {
return DROPBEAR_FAILURE;
} else {
return DROPBEAR_SUCCESS;
}
}

View File

@ -67,6 +67,7 @@ void __m_free(void* ptr);
void m_burn(void* data, unsigned int len); void m_burn(void* data, unsigned int len);
void setnonblocking(int fd); void setnonblocking(int fd);
void disallow_core(); void disallow_core();
int m_str_to_uint(const char* str, unsigned int *val);
/* Used to force mp_ints to be initialised */ /* Used to force mp_ints to be initialised */
#define DEF_MP_INT(X) mp_int X = {0, 0, 0, NULL} #define DEF_MP_INT(X) mp_int X = {0, 0, 0, NULL}

View File

@ -37,7 +37,7 @@ typedef struct runopts {
int listen_fwd_all; int listen_fwd_all;
#endif #endif
unsigned int recv_window; unsigned int recv_window;
time_t keepalive_secs; unsigned int keepalive_secs;
} runopts; } runopts;

View File

@ -284,16 +284,13 @@ void svr_getopts(int argc, char ** argv) {
if (recv_window_arg) { if (recv_window_arg) {
opts.recv_window = atol(recv_window_arg); opts.recv_window = atol(recv_window_arg);
if (opts.recv_window == 0 || opts.recv_window > MAX_RECV_WINDOW) if (opts.recv_window == 0 || opts.recv_window > MAX_RECV_WINDOW) {
{
dropbear_exit("Bad recv window '%s'", recv_window_arg); dropbear_exit("Bad recv window '%s'", recv_window_arg);
} }
} }
if (keepalive_arg) { if (keepalive_arg) {
opts.keepalive_secs = strtoul(keepalive_arg, NULL, 10); if (m_str_to_uint(keepalive_arg, &opts.keepalive_secs) == DROPBEAR_FAILURE) {
if (opts.keepalive_secs == 0 && errno == EINVAL)
{
dropbear_exit("Bad keepalive '%s'", keepalive_arg); dropbear_exit("Bad keepalive '%s'", keepalive_arg);
} }
} }