From 905b35111216c0ec6de50c03a446367d1d9ada3f Mon Sep 17 00:00:00 2001 From: "ethan.du" Date: Wed, 8 Apr 2020 18:04:54 +0800 Subject: [PATCH] bug fix some gcc not support scanf int8 and uint8 --- uiot/utils/json_token.c | 26 ++++++++++++++++++++++++-- uiot/utils/lite-utils.h | 8 -------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/uiot/utils/json_token.c b/uiot/utils/json_token.c index 828e697..9261e83 100644 --- a/uiot/utils/json_token.c +++ b/uiot/utils/json_token.c @@ -135,10 +135,26 @@ int LITE_get_int16(int16_t *value, char *src) { return (sscanf(src, "%" SCNi16, value) == 1) ? SUCCESS_RET : FAILURE_RET; } +/* NOTICE: scanning 8-bit types requires use of the hh specifier + * which is only supported on newlib platforms that + * are built with C99 I/O format support enabled. If the flag in + * newlib.h hasn't been set during configuration to indicate this, the 8-bit + * scanning format macros are disabled here as they result in undefined + * behaviour which can include memory overwrite. Overriding the flag after the + * library has been built is not recommended as it will expose the underlying + * undefined behaviour.so we use int16 and uint16 transfer to int8 and uint8 + */ int LITE_get_int8(int8_t *value, char *src) { - return (sscanf(src, "%" SCNi8, value) == 1) ? SUCCESS_RET : FAILURE_RET; + int16_t temp = 0; + if(1 != sscanf(src, "%" SCNi16, temp)) + { + return FAILURE_RET; + } + value = (int8_t)temp; + return SUCCESS_RET; } + int LITE_get_uint32(uint32_t *value, char *src) { return (sscanf(src, "%" SCNu32, value) == 1) ? SUCCESS_RET : FAILURE_RET; } @@ -148,7 +164,13 @@ int LITE_get_uint16(uint16_t *value, char *src) { } int LITE_get_uint8(uint8_t *value, char *src) { - return (sscanf(src, "%" SCNu8, value) == 1) ? SUCCESS_RET : FAILURE_RET; + uint16_t temp = 0; + if(1 != sscanf(src, "%" SCNu16, temp)) + { + return FAILURE_RET; + } + value = (uint8_t)temp; + return SUCCESS_RET; } int LITE_get_float(float *value, char *src) { diff --git a/uiot/utils/lite-utils.h b/uiot/utils/lite-utils.h index 269c1a4..ef7d193 100644 --- a/uiot/utils/lite-utils.h +++ b/uiot/utils/lite-utils.h @@ -57,14 +57,6 @@ extern "C" { } while(0) #endif -#ifndef SCNi8 -#define SCNi8 "hhi" -#endif - -#ifndef SCNu8 -#define SCNu8 "hhu" -#endif - char *LITE_strdup(const char *src); char *LITE_format_string(const char *fmt, ...); char *LITE_format_nstring(const int len, const char *fmt, ...);