kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit d4cec3427eaec78da10a853a2b1d83ba4384d575
parent ac345410dcbb3fbeb41b78803a1dba70767473e9
Author: Joris Vink <joris@coders.se>
Date:   Mon, 21 Jul 2014 01:16:03 +0200

Properly convert 64bit parameters.

Introduces kore_strtonum64() for just this purpose as to
not taint kore_strtonum() too much.

Diffstat:
includes/http.h | 16++++++++++++++++
includes/kore.h | 1+
src/http.c | 8++++----
src/utils.c | 31+++++++++++++++++++++++++++++++
4 files changed, 52 insertions(+), 4 deletions(-)

diff --git a/includes/http.h b/includes/http.h @@ -62,6 +62,16 @@ struct http_arg { *(t **)o = *(t **)v; \ } while (0); +#define COPY_ARG_INT64(type, sign) \ + do { \ + int err; \ + type nval; \ + nval = (type)kore_strtonum64(q->s_value, sign, &err); \ + if (err != KORE_RESULT_OK) \ + return (KORE_RESULT_ERROR); \ + COPY_ARG_TYPE(&nval, len, type, out); \ + } while (0); + #define COPY_ARG_INT(min, max, type) \ do { \ int err; \ @@ -81,6 +91,12 @@ struct http_arg { } \ } while (0); +#define COPY_AS_INTTYPE_64(type, sign) \ + do { \ + CACHE_STRING(); \ + COPY_ARG_INT64(type, sign); \ + } while (0); + #define COPY_AS_INTTYPE(min, max, type) \ do { \ CACHE_STRING(); \ diff --git a/includes/kore.h b/includes/kore.h @@ -401,6 +401,7 @@ time_t kore_date_to_time(char *); char *kore_time_to_date(time_t); char *kore_strdup(const char *); void kore_log(int, const char *, ...); +u_int64_t kore_strtonum64(const char *, int, int *); void kore_strlcpy(char *, const char *, size_t); void kore_server_disconnect(struct connection *); int kore_split_string(char *, char *, char **, size_t); diff --git a/src/http.c b/src/http.c @@ -634,11 +634,11 @@ http_argument_get(struct http_request *req, const char *name, COPY_AS_INTTYPE(0, UINT_MAX, u_int32_t); return (KORE_RESULT_OK); case HTTP_ARG_TYPE_INT64: - COPY_AS_INTTYPE(LONG_MIN, LONG_MAX, u_int64_t); - break; + COPY_AS_INTTYPE_64(int64_t, 1); + return (KORE_RESULT_OK); case HTTP_ARG_TYPE_UINT64: - COPY_AS_INTTYPE(0, ULONG_MAX, u_int64_t); - break; + COPY_AS_INTTYPE_64(u_int64_t, 0); + return (KORE_RESULT_OK); case HTTP_ARG_TYPE_STRING: CACHE_STRING(); *out = q->s_value; diff --git a/src/utils.c b/src/utils.c @@ -14,6 +14,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <limits.h> + #include "kore.h" static struct { @@ -123,6 +125,35 @@ kore_strtonum(const char *str, int base, long long min, long long max, int *err) return (l); } +u_int64_t +kore_strtonum64(const char *str, int sign, int *err) +{ + u_int64_t l; + char *ep; + + errno = 0; + l = strtoull(str, &ep, 10); + if (errno != 0 || str == ep || *ep != '\0') { + *err = KORE_RESULT_ERROR; + return (0); + } + + if (sign) { + if ((int64_t)l < LLONG_MIN || l > LLONG_MAX) { + *err = KORE_RESULT_ERROR; + return (0); + } + } else { + if ((int64_t)l < 0 || l > ULLONG_MAX) { + *err = KORE_RESULT_ERROR; + return (0); + } + } + + *err = KORE_RESULT_OK; + return (l); +} + int kore_split_string(char *input, char *delim, char **out, size_t ele) {