Quellcode durchsuchen

Merge pull request #24 from ucloud/bug_fix_some_gcc_not_support_scanf_int8

bug fix some gcc not support scanf int8 and uint8
ethanDu1 vor 5 Jahren
Ursprung
Commit
0707cbb7c7
2 geänderte Dateien mit 24 neuen und 10 gelöschten Zeilen
  1. 24 2
      uiot/utils/json_token.c
  2. 0 8
      uiot/utils/lite-utils.h

+ 24 - 2
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) {

+ 0 - 8
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, ...);