Skip to content

Segmentation fault occurred when data is unpacked using object_hook #28

@drednout

Description

@drednout

A segmentation fault occurred when I try to execute following script:

import datetime

import msgpack

useful_dict = {
    "id": 1,
    "nickname": "foo",
    "created": datetime.datetime.now(),
    "updated": datetime.datetime.now(),
}
def decode_datetime(obj):
    if b'__datetime__' in obj:
        obj = datetime.datetime.strptime("%Y%m%dT%H:%M:%S.%f")
    return obj

def encode_datetime(obj):
    if isinstance(obj, datetime.datetime):
        return {'__datetime__': True, 'as_str': obj.strftime("%Y%m%dT%H:%M:%S.%f")}
    return obj

print("Dict before packing: %s" % str(useful_dict))
packed_dict = msgpack.packb(useful_dict, default=encode_datetime)
this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime)
print("Dict after packing/unpacking: %s" % str(this_dict_again))
python test_default.py  
Dict before packing: {'updated': datetime.datetime(2012, 10, 12, 10, 47, 34, 405448), 'nickname': 'foo', 'id': 1, 'created': datetime.datetime(2012, 10, 12, 10, 47, 34, 405413)}
[1]    10601 segmentation fault (core dumped)  python test_default.py

After short investigation using gdb and looking to the source code the reason became clear.
It happens because of insufficient error checking during unpacking process. A problem occurred when some user data structure packed into dictionary.
I've solved the problem in my forked repo and will try to create a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions