Skip to content

Commit

Permalink
Merge pull request armink#14 from marckwei/master
Browse files Browse the repository at this point in the history
Replace strcpy with strncpy
  • Loading branch information
armink authored Nov 18, 2020
2 parents 5674312 + 20efcef commit b112f04
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions struct2json/inc/s2jdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ typedef struct {

#define S2J_STRUCT_GET_string_ELEMENT(to_struct, from_json, _element) \
json_temp = cJSON_GetObjectItem(from_json, #_element); \
if (json_temp) strcpy((to_struct)->_element, json_temp->valuestring);
if (json_temp) strncpy((to_struct)->_element, json_temp->valuestring,sizeof((to_struct)->_element));

#define S2J_STRUCT_GET_string_ELEMENT_EX(to_struct, from_json, _element, _defval) \
{ \
if (from_json) { \
json_temp = cJSON_GetObjectItem(from_json, #_element); \
if (json_temp) strcpy((to_struct)->_element, json_temp->valuestring); \
else strcpy((to_struct)->_element, _defval); \
if (json_temp) strncpy((to_struct)->_element, json_temp->valuestring,sizeof((to_struct)->_element)); \
else strncpy((to_struct)->_element, _defval,sizeof((to_struct)->_element)); \
} else { \
strcpy((to_struct)->_element, _defval); \
strncpy((to_struct)->_element, _defval,sizeof((to_struct)->_element)); \
} \
}

Expand All @@ -90,7 +90,7 @@ typedef struct {
(to_struct)->_element[index] = from_json->valueint;

#define S2J_STRUCT_ARRAY_GET_string_ELEMENT(to_struct, from_json, _element, index) \
strcpy((to_struct)->_element[index], from_json->valuestring);
strncpy((to_struct)->_element[index], from_json->valuestring,sizeof((to_struct)->_element));

#define S2J_STRUCT_ARRAY_GET_double_ELEMENT(to_struct, from_json, _element, index) \
(to_struct)->_element[index] = from_json->valuedouble;
Expand All @@ -103,8 +103,8 @@ typedef struct {
else (to_struct)->_element[index] = _defval;

#define S2J_STRUCT_ARRAY_GET_string_ELEMENT_EX(to_struct, from_json, _element, index, _defval) \
if (from_json) strcpy((to_struct)->_element[index], from_json->valuestring); \
else strcpy((to_struct)->_element[index], _defval);
if (from_json) strncpy((to_struct)->_element[index], from_json->valuestring,sizeof((to_struct)->_element)); \
else strncpy((to_struct)->_element[index], _defval,sizeof((to_struct)->_element));

#define S2J_STRUCT_ARRAY_GET_double_ELEMENT_EX(to_struct, from_json, _element, index) \
if (from_json) (to_struct)->_element[index] = from_json->valuedouble; \
Expand Down

0 comments on commit b112f04

Please sign in to comment.