[zeromq-dev] zmsg.hpp

Martin Lucina mato at kotelna.sk
Wed May 4 13:47:19 CEST 2011


ilejncs at narod.ru said:
> 04.05.2011, 15:33, "Martin Lucina" <mato at kotelna.sk>:
> > ilejncs at narod.ru said:
> >
> >>  Actually I still don't understand if it is guaranteed 0 is present, or code like
> >>  ==
> >>  char s[VERY_LONG];
> >>  sock.recv(&msg);
> >>  strcpy(s, msg.data());
> >>  s[msg.length()] = 0;
> >>  ==
> >>  relies on the destiny.
> >>
> >>  Asking just out of curiosity.
> >
> > Quoting zmq(7):
> >
> >    "A 0MQ message is a discrete unit of data passed between applications or
> >    components of the same application. 0MQ messages have no internal structure and
> >    from the point of view of 0MQ itself they are considered to be opaque binary
> >    data."
> >
> > Opaque binary data is just that. No guarantees on format, it's the
> > application that defines it.
> >
> > Cf. also the FAQ "Does ØMQ include APIs for serializing data to/from the
> > wire representation?" at http://www.zeromq.org/area:faq.
> >
> > In other words, if A (a C application) sends B (a C application) an ASCIIZ
> > string, the \0 will be there on B's end. If A' (a Python application) sends
> > B a Python string, the \0 will NOT be there on B's end.
> >
> > If you want portable data formats you need to define one and stick to it
> > :-)
> 
> Yes, of course.
> 
> What I don't understand is why Pieter confirmed that 
> the code pretty much like quoted is valid and "s[msg.length()] = 0;" makes sense.
> 
> The only way to explain is to assume 0 is present somewhere after a message.
> In this case strcpy is safe, though string must be properly terminated after copy.
> 
> Again, it is just curiosity - I want portable data formats and applications do not crash ;)

Ah.

s[msg.length()] = 0 is standard C boilerplate for "If the
message has no \0 termination, stick one at the end of the buffer just in
case."

-mato



More information about the zeromq-dev mailing list