Ver código fonte

fix: guard against empty key value in `parse_object_body`

Rickard Andersson 1 ano atrás
pai
commit
cfa3765d50
1 arquivos alterados com 10 adições e 6 exclusões
  1. 10 6
      core/encoding/json/parser.odin

+ 10 - 6
core/encoding/json/parser.odin

@@ -264,13 +264,17 @@ parse_object_body :: proc(p: ^Parser, end_token: Token_Kind) -> (obj: Object, er
 			return
 		}
 
-		insert_success := runtime.map_insert(&obj, key, elem)
-		// NOTE(gonz): we'd rather check specifically for an allocation error here but
-		// `map_insert` doesn't differentiate; we can only check for `nil`
-		if insert_success == nil {
-			return nil, .Out_Of_Memory
+		// NOTE(gonz): There are code paths for which this traversal ends up
+		// inserting empty key/values into the object and for those we do not
+		// want to allocate anything
+		if key != "" {
+			reserve_error := reserve(&obj, len(obj) + 1)
+			if reserve_error == mem.Allocator_Error.Out_Of_Memory {
+				return nil, .Out_Of_Memory
+			}
+			obj[key] = elem
 		}
-		
+
 		if parse_comma(p) {
 			break
 		}