• Daniel Martí's avatar
    encoding/json: fix "data changed underfoot?" panic · 5eff6bfd
    Daniel Martí authored
    Given a program as follows:
    
    	data := []byte(`{"F": {
    		"a": 2,
    		"3": 4
    	}}`)
    	json.Unmarshal(data, &map[string]map[int]int{})
    
    The JSON package should error, as "a" is not a valid integer. However,
    we'd encounter a panic:
    
    	panic: JSON decoder out of sync - data changing underfoot?
    
    The reason was that decodeState.object would return a nil error on
    encountering the invalid map key string, while saving the key type error
    for later. This broke if we were inside another object, as we would
    abruptly end parsing the nested object, leaving the decoder in an
    unexpected state.
    
    To fix this, simply avoid storing the map element and continue decoding
    the object, to leave the decoder state exactly as if we hadn't seen an
    invalid key type.
    
    This affected both signed and unsigned integer keys, so fix both and add
    two test cases.
    
    Updates #28189.
    
    Change-Id: I8a6204cc3ff9fb04ed769df7a20a824c8b94faff
    Reviewed-on: https://go-review.googlesource.com/c/142518Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
    5eff6bfd
Name
Last commit
Last update
.github Loading commit data...
api Loading commit data...
doc Loading commit data...
lib/time Loading commit data...
misc Loading commit data...
src Loading commit data...
test Loading commit data...
.gitattributes Loading commit data...
.gitignore Loading commit data...
AUTHORS Loading commit data...
CONTRIBUTING.md Loading commit data...
CONTRIBUTORS Loading commit data...
LICENSE Loading commit data...
PATENTS Loading commit data...
README.md Loading commit data...
favicon.ico Loading commit data...
robots.txt Loading commit data...