* lib/getdef.c: Use getlong instead of strtol/strtoul.

* libmisc/getlong, lib/getlong.c, libmisc/Makefile.am,
	lib/Makefile.am: getlong.c moved from libmisc/ to lib/.
This commit is contained in:
nekral-guest
2009-04-10 22:35:26 +00:00
parent 1c97cf5c83
commit 84f5ca951c
5 changed files with 50 additions and 12 deletions

View File

@@ -18,6 +18,7 @@ libshadow_la_SOURCES = \
getdef.c \
getdef.h \
get_gid.c \
getlong.c \
get_uid.c \
groupio.c \
groupmem.c \

View File

@@ -193,6 +193,7 @@ bool getdef_bool (const char *item)
int getdef_num (const char *item, int dflt)
{
struct itemdef *d;
long val;
if (!def_loaded) {
def_load ();
@@ -203,8 +204,16 @@ int getdef_num (const char *item, int dflt)
return dflt;
}
return (int) strtol (d->value, (char **) NULL, 0);
/* TODO: check for errors */
if ( (getlong (d->value, &val) == 0)
|| (val > INT_MAX)
|| (val < INT_MIN)) {
fprintf (stderr,
_("configuration error - cannot parse %s value: '%s'"),
item, d->value);
return dflt;
}
return (int) val;
}
@@ -219,6 +228,7 @@ int getdef_num (const char *item, int dflt)
unsigned int getdef_unum (const char *item, unsigned int dflt)
{
struct itemdef *d;
long val;
if (!def_loaded) {
def_load ();
@@ -229,8 +239,16 @@ unsigned int getdef_unum (const char *item, unsigned int dflt)
return dflt;
}
return (unsigned int) strtoul (d->value, (char **) NULL, 0);
/* TODO: check for errors */
if ( (getlong (d->value, &val) == 0)
|| (val < 0)
|| (val > INT_MAX)) {
fprintf (stderr,
_("configuration error - cannot parse %s value: '%s'"),
item, d->value);
return dflt;
}
return (unsigned int) val;
}
@@ -245,6 +263,7 @@ unsigned int getdef_unum (const char *item, unsigned int dflt)
long getdef_long (const char *item, long dflt)
{
struct itemdef *d;
long val;
if (!def_loaded) {
def_load ();
@@ -255,8 +274,14 @@ long getdef_long (const char *item, long dflt)
return dflt;
}
return strtol (d->value, (char **) NULL, 0);
/* TODO: check for errors */
if (getlong (d->value, &val) == 0) {
fprintf (stderr,
_("configuration error - cannot parse %s value: '%s'"),
item, d->value);
return dflt;
}
return val;
}
/*
@@ -270,6 +295,7 @@ long getdef_long (const char *item, long dflt)
unsigned long getdef_ulong (const char *item, unsigned long dflt)
{
struct itemdef *d;
long val;
if (!def_loaded) {
def_load ();
@@ -280,8 +306,15 @@ unsigned long getdef_ulong (const char *item, unsigned long dflt)
return dflt;
}
return (unsigned long) strtoul (d->value, (char **) NULL, 0);
/* TODO: check for errors */
if (getlong (d->value, &val) == 0) {
/* FIXME: we should have a getulong */
fprintf (stderr,
_("configuration error - cannot parse %s value: '%s'"),
item, d->value);
return dflt;
}
return val;
}
/*
@@ -354,9 +387,8 @@ static struct itemdef *def_find (const char *name)
*/
fprintf (stderr,
_
("configuration error - unknown item '%s' (notify administrator)\n"),
name);
_("configuration error - unknown item '%s' (notify administrator)\n"),
name);
SYSLOG ((LOG_CRIT, "unknown configuration item `%s'", name));
return (struct itemdef *) NULL;
}

52
lib/getlong.c Normal file
View File

@@ -0,0 +1,52 @@
/*
* Copyright (c) 2007 - 2009, Nicolas François
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the copyright holders or contributors may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <config.h>
#ident "$Id$"
#include <stdlib.h>
#include <errno.h>
#include "prototypes.h"
int getlong (const char *numstr, long int *result)
{
long val;
char *endptr;
errno = 0;
val = strtol (numstr, &endptr, 10);
if (('\0' == numstr) || ('\0' != *endptr) || (ERANGE == errno)) {
return 0;
}
*result = val;
return 1;
}