From 2af71fbc345aa19321c29fac2066686fefc81665 Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Sat, 5 Oct 2019 16:33:34 -0400 Subject: [PATCH] Refactor for safety. --- platform-independent/c/core/src/mpw-marshal-util.c | 2 ++ platform-independent/c/core/src/mpw-marshal.c | 6 +++--- platform-independent/c/core/src/mpw-marshal.h | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/platform-independent/c/core/src/mpw-marshal-util.c b/platform-independent/c/core/src/mpw-marshal-util.c index eae99cfd..4fabf682 100644 --- a/platform-independent/c/core/src/mpw-marshal-util.c +++ b/platform-independent/c/core/src/mpw-marshal-util.c @@ -201,6 +201,7 @@ void mpw_set_json_data( continue; } *(child = &data->children[data->children_count - 1]) = (MPMarshalledData){ .obj_key = mpw_strdup( entry.key ) }; + mpw_marshal_data_set_null( child, NULL ); } mpw_set_json_data( child, entry.val ); @@ -221,6 +222,7 @@ void mpw_set_json_data( continue; } *(child = &data->children[data->children_count - 1]) = (MPMarshalledData){ .arr_index = index }; + mpw_marshal_data_set_null( child, NULL ); } mpw_set_json_data( child, json_object_array_get_idx( obj, index ) ); diff --git a/platform-independent/c/core/src/mpw-marshal.c b/platform-independent/c/core/src/mpw-marshal.c index 5fab8e59..3cba1d7a 100644 --- a/platform-independent/c/core/src/mpw-marshal.c +++ b/platform-independent/c/core/src/mpw-marshal.c @@ -112,7 +112,7 @@ MPMarshalledFile *mpw_marshal_file( if (!(file = malloc( sizeof( MPMarshalledFile ) ))) return NULL; - *file = (MPMarshalledFile){}; + *file = (MPMarshalledFile){ .info = NULL, .data = NULL, .error = (MPMarshalError){ .type = MPMarshalSuccess, .message = NULL } }; } if (data && data != file->data) { @@ -986,8 +986,8 @@ MPMarshalledUser *mpw_marshal_auth( // Section: "user" bool fileRedacted = mpw_marshal_data_get_bool( file->data, "export", "redacted", NULL ) || mpw_marshal_data_is_null( file->data, "export", "redacted", NULL ); - MPAlgorithmVersion - algorithm = mpw_default_n( MPAlgorithmVersionCurrent, mpw_marshal_data_get_num( file->data, "user", "algorithm", NULL ) ); + MPAlgorithmVersion algorithm = + mpw_default_n( MPAlgorithmVersionCurrent, mpw_marshal_data_get_num( file->data, "user", "algorithm", NULL ) ); if (algorithm < MPAlgorithmVersionFirst || algorithm > MPAlgorithmVersionLast) { file->error = (MPMarshalError){ MPMarshalErrorIllegal, mpw_str( "Invalid user algorithm: %u", algorithm ) }; return NULL; diff --git a/platform-independent/c/core/src/mpw-marshal.h b/platform-independent/c/core/src/mpw-marshal.h index 9cfba17d..2766b076 100644 --- a/platform-independent/c/core/src/mpw-marshal.h +++ b/platform-independent/c/core/src/mpw-marshal.h @@ -29,7 +29,7 @@ MP_LIBS_END //// Types. #define mpw_default( __default, __value ) ({ __typeof__ (__default) _v = (__typeof__ (__default))(__value); _v = _v? _v: __default; }) -#define mpw_default_n( __default, __num ) ({ __typeof__ (__default) _n = (__typeof__ (__default))(__num); _n = !isnan( _n )? _n: __default; }) +#define mpw_default_n( __default, __num ) ({ __typeof__ (__num) _n = (__num); !isnan( _n )? (__typeof__ (__default))_n: __default; }) typedef mpw_enum( unsigned int, MPMarshalFormat ) { /** Do not marshal. */