Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Error unpacking list with some instances of floating-point numbers. #2

Closed
xboard opened this Issue Oct 24, 2012 · 1 comment

Comments

Projects
None yet
2 participants

xboard commented Oct 24, 2012

I persisted a serialized list of floating-point numbers using python:

import msgpack

with open('msgpack.tst', 'wb') as file:
    file.write(msgpack.packb([1.0,  2.0]))

and then tested reading it from a very simple c code:

#include <msgpack.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]){
  int BUFFER_SIZE = 1024;
  FILE* source;
  int count = 0;
  char* buffer  = (char*) malloc(BUFFER_SIZE);
  source = fopen("msgpack.tst","rb");
  if(source){
      n = fread(buffer,BUFFER_SIZE, 1, source);
      count = strlen(buffer);
  }else{
     printf("Failed to open file\n");
     return 1;
  }

  /* deserializes it. */
  msgpack_unpacked msg;
  msgpack_unpacked_init(&msg);
  printf("buffer len: %d   value:%s\n",count, buffer);
  bool success = msgpack_unpack_next(&msg, buffer, count, NULL);
  if (success){
       printf("Success unpacking!\n");
       /* prints the deserialized object. */
       msgpack_object obj = msg.data;
       msgpack_object_print(stdout, obj);
       puts("\n");
  }else{
       printf("Unpacking failed!\n");
  }

}

To my surprise the unpacking fails:

buffer len: 12   value:���������@
Unpacking failed!

But if my python code serializes 'true' floating-point numbers as bellow:

import msgpack

with open('msgpack.tst', 'wb') as file:
    file.write(msgpack.packb([1.2345,  2.9876]))

Then the C code success unpacking this list of floating-point numbers:

buffer len: 19   value:��?����n���@���,<�
Sucess unpacking!
[1.234500, 2.987600]

Persisting lists of strings and list of integers also works fine.

Regards,

PS : Using Ubuntu 12.04.1, msgpack version is (0,2,2) for python and 0.5.5 for C.

Member

nobu-k commented Nov 29, 2012

Hi, you can't use strlen to get the length of the buffer because it may contain '\0'. If msgpack.tst only contains serialized data, you can obtain the length from the return value of fread.

@nobu-k nobu-k closed this Feb 28, 2014

redboltz pushed a commit to redboltz/msgpack-c that referenced this issue Aug 14, 2015

Merge pull request #2 from jonitis/feature_vector_of_non_default_cons…
…tructible_values

Support vectors of non-default-constructible values in c++11 mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment