Skip to content

ArrayEach + Set lost symbols #141

Description

@sergolius

Example: you have array of maps, and you need add extra fields to each element of array.
This may cause an issue, if maps are big enough.
After extending each map result array ([]byte) may reach out of range and you will lost some number of symbols in result.

Code example: https://play.golang.org/p/3QcQtupAfIr

p.s. Update data by slice(referencing) is not obvious.

Activity

  1. dcelasun commented on Sep 3, 2019

    @dcelasun

    @sergolius did you ever find a solution to this?

  2. sergolius commented on Sep 3, 2019

    @sergolius
    Author

    @dcelasun I didn't dig into it much.
    I copying original value to v inside of .ArrayEach to break reference and operate with copy:

    _, err := jsonparser.ArrayEach([]byte(data),
    	func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
    		v := make([]byte, len(value))
    		copy(v, value) 
    ...
                    v, _ = jsonparser.Set(v, []byte(`"new_value"`), "key")
    
  3. dcelasun commented on Sep 3, 2019

    @dcelasun

    Yeah, I've ended up doing the same. Thanks anyway.

  4. added a commit that references this issue on Jul 28, 2026
  5. buger commented on Jul 28, 2026

    @buger
    Owner

    Fixed in v1.3.1 — Set no longer corrupts the input buffer when growing a missing path. The append() that wrote into the caller's spare backing-array capacity is now guarded by allocating a fresh buffer on all mutation paths. Regression test: TestSetDoesNotMutateInputBackingArrayWhenGrowing in v131_regression_test.go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions