Skip to content

Commit

Permalink
lib: Prepare for strtoul_err(), strtoull_err() API change
Browse files Browse the repository at this point in the history
In order to still be bisectable when changing the API for the wrappers
strtoul_err() and strtoull_err() some preparations need to be performed.

Signed-off-by: Swen Schillig <[email protected]>
Reviewed-by: Ralph Boehme <[email protected]>
Reviewed-by: Christof Schmitt <[email protected]>
  • Loading branch information
sswen authored and slowfranklin committed Jun 30, 2019
1 parent 7fd0cd0 commit f2997ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
34 changes: 24 additions & 10 deletions lib/util/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,21 @@
* @param endptr [optional] reference to remainder of the string
* @param base base of the numbering scheme
* @param err error occured during conversion
* @flags controlling conversion feature
* @result result of the conversion as provided by strtoul
*
* The following flags are supported
* SMB_STR_STANDARD # raise error if negative or non-numeric
* SMB_STR_ALLOW_NEGATIVE # allow strings with a leading "-"
*
* The following errors are detected
* - wrong base
* - value overflow
* - string with a leading "-" indicating a negative number
* - no conversion due to empty string or not representing a number
*/
unsigned long int
strtoul_err(const char *nptr, char **endptr, int base, int *err)
smb_strtoul(const char *nptr, char **endptr, int base, int *err, int flags)
{
unsigned long int val;
int saved_errno = errno;
Expand Down Expand Up @@ -93,10 +98,12 @@ strtoul_err(const char *nptr, char **endptr, int base, int *err)
return val;
}

/* did we convert a negative "number" ? */
needle = strchr(nptr, '-');
if (needle != NULL && needle < tmp_endptr) {
*err = EINVAL;
if ((flags & SMB_STR_ALLOW_NEGATIVE ) == 0) {
/* did we convert a negative "number" ? */
needle = strchr(nptr, '-');
if (needle != NULL && needle < tmp_endptr) {
*err = EINVAL;
}
}

errno = saved_errno;
Expand All @@ -110,16 +117,21 @@ strtoul_err(const char *nptr, char **endptr, int base, int *err)
* @param endptr [optional] reference to remainder of the string
* @param base base of the numbering scheme
* @param err error occured during conversion
* @flags controlling conversion feature
* @result result of the conversion as provided by strtoull
*
* The following flags are supported
* SMB_STR_STANDARD # raise error if negative or non-numeric
* SMB_STR_ALLOW_NEGATIVE # allow strings with a leading "-"
*
* The following errors are detected
* - wrong base
* - value overflow
* - string with a leading "-" indicating a negative number
* - no conversion due to empty string or not representing a number
*/
unsigned long long int
strtoull_err(const char *nptr, char **endptr, int base, int *err)
smb_strtoull(const char *nptr, char **endptr, int base, int *err, int flags)
{
unsigned long long int val;
int saved_errno = errno;
Expand Down Expand Up @@ -148,10 +160,12 @@ strtoull_err(const char *nptr, char **endptr, int base, int *err)
return val;
}

/* did we convert a negative "number" ? */
needle = strchr(nptr, '-');
if (needle != NULL && needle < tmp_endptr) {
*err = EINVAL;
if ((flags & SMB_STR_ALLOW_NEGATIVE ) == 0) {
/* did we convert a negative "number" ? */
needle = strchr(nptr, '-');
if (needle != NULL && needle < tmp_endptr) {
*err = EINVAL;
}
}

errno = saved_errno;
Expand Down
9 changes: 7 additions & 2 deletions lib/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Utility functions for Samba
Copyright (C) Andrew Tridgell 1992-1999
Copyright (C) Jelmer Vernooij 2005
Copyright (C) Swen Schillig 2019
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -29,11 +30,15 @@
SMB_STR_ALLOW_NEGATIVE)

unsigned long int
strtoul_err(const char *nptr, char **endptr, int base, int *err);
smb_strtoul(const char *nptr, char **endptr, int base, int *err, int flags);

unsigned long long int
strtoull_err(const char *nptr, char **endptr, int base, int *err);
smb_strtoull(const char *nptr, char **endptr, int base, int *err, int flags);

#define strtoul_err(nptr, endptr, base, err) \
smb_strtoul(nptr, endptr, base, err, SMB_STR_STANDARD)
#define strtoull_err(nptr, endptr, base, err) \
smb_strtoull(nptr, endptr, base, err, SMB_STR_STANDARD)

/**
* Write dump of binary data to a callback
Expand Down

0 comments on commit f2997ad

Please sign in to comment.