33#include <assert.h>
44#include <ctype.h>
55#include <errno.h>
6+ #include <float.h>
67#include <stdbool.h>
78#include <stdint.h>
89#include <stdio.h>
910#include <stdlib.h>
1011#include <string.h>
11- #include <float.h>
1212
1313#include <flutter_embedder.h>
1414
@@ -957,118 +957,147 @@ int platch_decode_json(char *string, struct json_value *out) {
957957}
958958
959959int platch_decode (const uint8_t * buffer , size_t size , enum platch_codec codec , struct platch_obj * object_out ) {
960- struct json_value root_jsvalue ;
961- const uint8_t * buffer_cursor = buffer ;
962- size_t remaining = size ;
963960 int ok ;
964961
965962 if ((size == 0 ) && (buffer == NULL )) {
966963 object_out -> codec = kNotImplemented ;
967964 return 0 ;
968965 }
969966
967+ const uint8_t * buffer_cursor = buffer ;
968+ size_t remaining = size ;
969+
970970 object_out -> codec = codec ;
971971 switch (codec ) {
972- case kNotImplemented :
973- ASSERT_EQUALS (size , 0 );
974- ASSERT_EQUALS (buffer , NULL );
975- break ;
972+ case kNotImplemented : {
973+ if (size != 0 ) {
974+ return EINVAL ;
975+ }
976+ if (buffer != NULL ) {
977+ return EINVAL ;
978+ }
976979
977- case kStringCodec :;
978- /// buffer is a non-null-terminated, UTF8-encoded string.
979- /// it's really sad we have to allocate a new memory block for this, but we have to since string codec buffers are not null-terminated.
980+ break ;
981+ }
980982
981- char * string ;
982- if (!(string = malloc (size + 1 )))
983+ case kStringCodec : {
984+ char * string = malloc (size + 1 );
985+ if (string == NULL ) {
983986 return ENOMEM ;
984- memcpy (string , buffer , size );
987+ }
988+
989+ strncpy (string , (char * ) buffer , size );
985990 string [size ] = '\0' ;
986991
987992 object_out -> string_value = string ;
988-
989993 break ;
990- case kBinaryCodec :
994+ }
995+ case kBinaryCodec : {
996+ if (size == 0 ) {
997+ return EINVAL ;
998+ }
999+ if (buffer == NULL ) {
1000+ return EINVAL ;
1001+ }
1002+
9911003 object_out -> binarydata = buffer ;
9921004 object_out -> binarydata_size = size ;
993-
9941005 break ;
995- case kJSONMessageCodec :
996- ok = platch_decode_value_json ((char * ) buffer , size , NULL , NULL , & (object_out -> json_value ));
997- if (ok != 0 )
1006+ }
1007+ case kJSONMessageCodec : {
1008+ ok = platch_decode_value_json ((char * ) buffer , size , NULL , NULL , & object_out -> json_value );
1009+ if (ok != 0 ) {
9981010 return ok ;
1011+ }
9991012
10001013 break ;
1001- case kJSONMethodCall :;
1002- ok = platch_decode_value_json ((char * ) buffer , size , NULL , NULL , & root_jsvalue );
1003- if (ok != 0 )
1004- return ok ;
1014+ }
1015+ case kJSONMethodCall : {
1016+ struct json_value root ;
10051017
1006- if (root_jsvalue .type != kJsonObject )
1007- return EBADMSG ;
1018+ ok = platch_decode_value_json ((char * ) buffer , size , NULL , NULL , & root );
1019+ if (ok != 0 ) {
1020+ return ok ;
1021+ }
10081022
1009- for (int i = 0 ; i < root_jsvalue .size ; i ++ ) {
1010- if ((streq (root_jsvalue .keys [i ], "method" )) && (root_jsvalue .values [i ].type == kJsonString )) {
1011- object_out -> method = root_jsvalue .values [i ].string_value ;
1012- } else if (streq (root_jsvalue .keys [i ], "args" )) {
1013- object_out -> json_arg = root_jsvalue .values [i ];
1014- } else
1015- return EBADMSG ;
1023+ if (root .type != kJsonObject ) {
1024+ platch_free_json_value (& root , true);
1025+ return EINVAL ;
10161026 }
10171027
1018- platch_free_json_value (& root_jsvalue , true);
1028+ for (int i = 0 ; i < root .size ; i ++ ) {
1029+ if ((streq (root .keys [i ], "method" )) && (root .values [i ].type == kJsonString )) {
1030+ object_out -> method = root .values [i ].string_value ;
1031+ } else if (streq (root .keys [i ], "args" )) {
1032+ object_out -> json_arg = root .values [i ];
1033+ } else {
1034+ return EINVAL ;
1035+ }
1036+ }
10191037
1038+ platch_free_json_value (& root , true);
10201039 break ;
1040+ }
10211041 case kJSONMethodCallResponse : {
1022- ok = platch_decode_value_json ((char * ) buffer , size , NULL , NULL , & root_jsvalue );
1042+ struct json_value root ;
1043+
1044+ ok = platch_decode_value_json ((char * ) buffer , size , NULL , NULL , & root );
10231045 if (ok != 0 ) {
10241046 return ok ;
10251047 }
10261048
1027- if (root_jsvalue .type != kJsonArray ) {
1028- platch_free_json_value (& root_jsvalue , true);
1029- return EBADMSG ;
1049+ if (root .type != kJsonArray ) {
1050+ platch_free_json_value (& root , true);
1051+ return EINVAL ;
10301052 }
10311053
1032- if (root_jsvalue .size == 1 ) {
1054+ if (root .size == 1 ) {
10331055 object_out -> success = true;
1034- object_out -> json_result = root_jsvalue .array [0 ];
1035- platch_free_json_value (& root_jsvalue , true);
1036- } else if ((root_jsvalue .size == 3 ) && (root_jsvalue .array [0 ].type == kJsonString ) &&
1037- ((root_jsvalue .array [1 ].type == kJsonString ) || (root_jsvalue .array [1 ].type == kJsonNull ))) {
1056+ object_out -> json_result = root .array [0 ];
1057+ } else if ((root .size == 3 ) && (root .array [0 ].type == kJsonString ) &&
1058+ ((root .array [1 ].type == kJsonString ) || (root .array [1 ].type == kJsonNull ))) {
10381059 object_out -> success = false;
1039- object_out -> error_code = root_jsvalue .array [0 ].string_value ;
1040- object_out -> error_msg = root_jsvalue .array [1 ].string_value ;
1041- object_out -> json_error_details = root_jsvalue .array [2 ];
1042- platch_free_json_value (& root_jsvalue , true);
1060+ object_out -> error_code = root .array [0 ].string_value ;
1061+ object_out -> error_msg = root .array [1 ].string_value ;
1062+ object_out -> json_error_details = root .array [2 ];
10431063 } else {
1044- platch_free_json_value (& root_jsvalue , true);
1045- return EBADMSG ;
1064+ platch_free_json_value (& root , true);
1065+ return EINVAL ;
10461066 }
10471067
1068+ platch_free_json_value (& root , true);
10481069 break ;
10491070 }
1050- case kStandardMessageCodec :
1071+ case kStandardMessageCodec : {
10511072 ok = platch_decode_value_std (& buffer_cursor , & remaining , & object_out -> std_value );
1052- if (ok != 0 )
1073+ if (ok != 0 ) {
10531074 return ok ;
1075+ }
1076+
10541077 break ;
1055- case kStandardMethodCall :;
1078+ }
1079+ case kStandardMethodCall : {
10561080 struct std_value methodname ;
10571081
10581082 ok = platch_decode_value_std (& buffer_cursor , & remaining , & methodname );
1059- if (ok != 0 )
1083+ if (ok != 0 ) {
10601084 return ok ;
1085+ }
1086+
10611087 if (methodname .type != kStdString ) {
10621088 platch_free_value_std (& methodname );
1063- return EBADMSG ;
1089+ return EINVAL ;
10641090 }
1091+
10651092 object_out -> method = methodname .string_value ;
10661093
10671094 ok = platch_decode_value_std (& buffer_cursor , & remaining , & object_out -> std_arg );
1068- if (ok != 0 )
1095+ if (ok != 0 ) {
10691096 return ok ;
1097+ }
10701098
10711099 break ;
1100+ }
10721101 case kStandardMethodCallResponse : {
10731102 ok = _read_u8 (& buffer_cursor , (uint8_t * ) & object_out -> success , & remaining );
10741103 if (ok != 0 ) {
@@ -1077,8 +1106,9 @@ int platch_decode(const uint8_t *buffer, size_t size, enum platch_codec codec, s
10771106
10781107 if (object_out -> success ) {
10791108 ok = platch_decode_value_std (& buffer_cursor , & remaining , & (object_out -> std_result ));
1080- if (ok != 0 )
1109+ if (ok != 0 ) {
10811110 return ok ;
1111+ }
10821112 } else {
10831113 struct std_value error_code , error_msg ;
10841114
@@ -1107,9 +1137,10 @@ int platch_decode(const uint8_t *buffer, size_t size, enum platch_codec codec, s
11071137 platch_free_value_std (& object_out -> std_error_details );
11081138 platch_free_value_std (& error_code );
11091139 platch_free_value_std (& error_msg );
1110- return EBADMSG ;
1140+ return EINVAL ;
11111141 }
11121142 }
1143+
11131144 break ;
11141145 }
11151146 default : return EINVAL ;
@@ -1128,28 +1159,58 @@ int platch_encode(struct platch_obj *object, uint8_t **buffer_out, size_t *size_
11281159 * buffer_out = NULL ;
11291160
11301161 switch (object -> codec ) {
1131- case kNotImplemented :
1162+ case kNotImplemented : {
11321163 * size_out = 0 ;
11331164 * buffer_out = NULL ;
11341165 return 0 ;
1135- case kStringCodec : size = strlen (object -> string_value ); break ;
1136- case kBinaryCodec :
1166+ }
1167+ case kStringCodec : {
1168+ * buffer_out = (uint8_t * ) strdup (object -> string_value );
1169+ if (buffer_out == NULL ) {
1170+ return ENOMEM ;
1171+ }
1172+
1173+ * size_out = strlen (object -> string_value );
1174+ return 0 ;
1175+ }
1176+ case kBinaryCodec : {
11371177 /// FIXME: Copy buffer instead
11381178 * buffer_out = (uint8_t * ) object -> binarydata ;
11391179 * size_out = object -> binarydata_size ;
11401180 return 0 ;
1141- case kJSONMessageCodec :
1181+ }
1182+ case kJSONMessageCodec : {
11421183 size = platch_calc_value_size_json (& (object -> json_value ));
11431184 size += 1 ; // JSONMsgCodec uses sprintf, which null-terminates strings,
11441185 // so lets allocate one more byte for the last null-terminator.
11451186 // this is decremented again in the second switch-case, so flutter
11461187 // doesn't complain about a malformed message.
1188+
1189+ buffer = malloc (size );
1190+ if (buffer == NULL ) {
1191+ return ENOMEM ;
1192+ }
1193+
1194+ ok = platch_write_value_to_buffer_json (& (object -> json_value ), & buffer_cursor );
1195+ if (ok != 0 ) {
1196+ goto free_buffer_and_return_ok ;
1197+ }
1198+
11471199 break ;
1148- case kStandardMessageCodec :
1200+ }
1201+ case kStandardMessageCodec : {
11491202 ok = platch_calc_value_size_std (& (object -> std_value ), & size );
1150- if (ok != 0 )
1203+ if (ok != 0 ) {
11511204 return ok ;
1152- break ;
1205+ }
1206+
1207+ buffer = malloc (size );
1208+ if (buffer == NULL ) {
1209+ return ENOMEM ;
1210+ }
1211+
1212+ buffer_cursor = buffer ;
1213+ }
11531214 case kStandardMethodCall :
11541215 stdmethod .type = kStdString ;
11551216 stdmethod .string_value = object -> method ;
@@ -2254,7 +2315,7 @@ ATTR_PURE bool raw_std_value_equals(const struct raw_std_value *a, const struct
22542315 length = raw_std_value_get_size (a );
22552316 const float * a_floats = raw_std_value_as_float32array (a );
22562317 const float * b_floats = raw_std_value_as_float32array (b );
2257-
2318+
22582319 PRAGMA_DIAGNOSTIC_PUSH
22592320 PRAGMA_DIAGNOSTIC_IGNORED ("- Wfloat - equal ")
22602321 for (int i = 0 ; i < length ; i ++ ) {
0 commit comments