GithubHelp home page GithubHelp logo

Comments (1)

renatotoyama avatar renatotoyama commented on May 29, 2024

Thanks for the quick update, but unfortunately, since it still doesn't unpack the value upon a non-existing field, the next iterations will be unpacking the value as if it was the name, and if this value and the rest of the values are not strings, it will skip the rest of the map (while leaving the last value in MsgStream). If there are string values, it will try to use the current map value as name, and if it fails to find name in val again, it will return using name as name, correctly, except i will be one over what it should be, thus truncating the end of the map (but still leaving the leftover pairs in the MsgStream), and possibly wrecking havoc on later values.

proc unpack_type*[ByteStream; T: tuple|object](s: ByteStream, val: var T) =
  template dry_and_wet(): untyped =
    when defined(msgpack_obj_to_map):
      let len = s.unpack_map()
      var name: string
      for i in 0..len-1:
        if not s.is_string:
          s.skip_msg()
          s.skip_msg()
          continue
        unpack_proxy(name) # it will try to unpack the map value here on next iteration
        for field, value in fieldPairs(val):
          if field == name:
            unpack_proxy(value) # It will still skip this line if name is not found in val

What should be done is, if unpack_proxy(value) is not called, s.skip_msg() must be called.

Additional info: the reason your test passed is because your map has length 4, your first non-existing field's value is a string, and you don't check the last value of the map (since this last value will be skipped/truncated).

    var a = anyMap()
    a[anyInt(123)] = anyString("non-string-field") # this will be skipped, but will offset i to one over, which will ultimately, stop without unpacking the last item in map
    a["name"] = anyString("apple")
    a["color"] = anyInt(1001)
    a["someInt"] = anyInt(123) # this last item will not be unpacked

    var s = MsgStream.init(fromAny(a), MSGPACK_OBJ_TO_MAP)
    var x = s.unpack(Fruit)
    check x.name == "apple"
    check x.color == 1001
    # No someInt check, which would have failed with the default value, since unpacking was not performed

Edit: Created a PR, along with a test that should catch this scenario.

from msgpack4nim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.