Skip to content

Commit

Permalink
COMMON: Error on parseString() with '-' to unsigned value
Browse files Browse the repository at this point in the history
strtoul() and friends silently convert such a value to its unsigned
counterpart. We don't want that.
  • Loading branch information
DrMcCoy committed Dec 23, 2016
1 parent 2c62c70 commit 4196e17
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/common/strutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,18 @@ UString debugTag(uint32 tag, bool trim) {

// Helper functions for parseString()

static void errorOnSign(const char *str) {
if (strchr(str, '-') != 0)
errno = ERANGE;
}

static inline void parse(const char *nptr, char **endptr, signed long long &value) {
value = strtoll(nptr, endptr, 0);
}

static inline void parse(const char *nptr, char **endptr, unsigned long long &value) {
errorOnSign(nptr);

value = strtoull(nptr, endptr, 0);
}

Expand All @@ -135,6 +142,8 @@ static inline void parse(const char *nptr, char **endptr, signed long &value) {
}

static inline void parse(const char *nptr, char **endptr, unsigned long &value) {
errorOnSign(nptr);

value = strtoul(nptr, endptr, 0);
}

Expand All @@ -147,6 +156,8 @@ static inline void parse(const char *nptr, char **endptr, signed int &value) {
}

static inline void parse(const char *nptr, char **endptr, unsigned int &value) {
errorOnSign(nptr);

unsigned long tmp = strtoul(nptr, endptr, 0);
if (tmp > UINT_MAX)
errno = ERANGE;
Expand All @@ -163,6 +174,8 @@ static inline void parse(const char *nptr, char **endptr, signed short &value) {
}

static inline void parse(const char *nptr, char **endptr, unsigned short &value) {
errorOnSign(nptr);

unsigned long tmp = strtoul(nptr, endptr, 0);
if (tmp > USHRT_MAX)
errno = ERANGE;
Expand All @@ -179,6 +192,8 @@ static inline void parse(const char *nptr, char **endptr, signed char &value) {
}

static inline void parse(const char *nptr, char **endptr, unsigned char &value) {
errorOnSign(nptr);

unsigned long tmp = strtoul(nptr, endptr, 0);
if (tmp > UCHAR_MAX)
errno = ERANGE;
Expand Down

0 comments on commit 4196e17

Please sign in to comment.