Mercurial > hg > batmud > maputils
changeset 1896:36074f4e95c7
Implement self-rolled mapAtoI() to replace atoi() usage in the map search
function, in order to get rid of the data modification (to add NUL byte for
atoi()).
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 06 Nov 2017 03:29:36 +0200 |
parents | f15d00bb9bd2 |
children | 38cc5e420118 |
files | mapsearch.c |
diffstat | 1 files changed, 31 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/mapsearch.c Mon Nov 06 03:13:01 2017 +0200 +++ b/mapsearch.c Mon Nov 06 03:29:36 2017 +0200 @@ -791,6 +791,35 @@ } +int mapAtoI(const char *str, const size_t len) +{ + int value = 0; + size_t i = 0; + BOOL neg = FALSE; + + while (th_isspace(str[i])) i++; + + if (str[i] == '-') + { + neg = TRUE; + i++; + } + + for (; i < len; i++) + { + if (str[i] >= '0' && str[i] <= '9') + { + value *= 10; + value += str[i] - '0'; + } + else + break; + } + + return neg ? -value : value; +} + + // // This wrapper function for lws_write exists because of the // libwebsockets' requirement for pre-pad of the data buffer @@ -811,7 +840,7 @@ } -void mapPerformSearch(struct lws *wsi, unsigned char *data, const size_t len, char **verr) +void mapPerformSearch(struct lws *wsi, const unsigned char *data, const size_t len, char **verr) { static const char *cleanChars = " *@?%C"; size_t ncleanChars = strlen(cleanChars); @@ -833,9 +862,7 @@ goto out; } - data[offs++] = 0; - - maxMatches = atoi((char *) data); + maxMatches = mapAtoI((char *) data, offs); mapMSG(2, "Requested %d matches.\n", maxMatches); if (maxMatches < 1 || maxMatches > SET_MAX_MATCHES)