Bug #2552
closed
Block::fromBuffer(ConstBufferPtr, size_t, Block&) unexpected decode error
Added by Junxiao Shi almost 10 years ago.
Updated almost 10 years ago.
Description
Snippet to reproduce:
// g++ -std=c++0x x.cpp `pkg-config --cflags --libs libndn-cxx`
#include <ndn-cxx/encoding/block.hpp>
using ndn::Buffer;
using ndn::BufferPtr;
using ndn::Block;
int
main()
{
BufferPtr buffer = std::make_shared<Buffer>(4);
uint8_t* buf = buffer->get();
buf[0] = 0x01;
buf[1] = 0x02;
buf[2] = 0xcc;
buf[3] = 0xdd;
Block block1;
Block block2;
std::cout << Block::fromBuffer(buffer, 4, block1)
<< Block::fromBuffer(buf, 4, block2)
<< std::endl;
return 0;
}
Expected: 11
Actual: 01
- Blocks Feature #2261: Don't use exceptions in PartialMessageStore::receiveNdnlpData added
You misunderstood what fromBuffer(const ConstBufferPtr&, size_t, Block&);
method is doing. The two fromBuffer overloads are not equivalent to each other and have different use cases:
fromBuffer(const uint8_t* buffer, size_t maxSize, Block& block);
should be used when raw buffer is available and you want to create a Block from it (internally, it will create shared_ptr and copy the supplied data.
fromBuffer(const ConstBufferPtr& wire, size_t offset, Block& block)
should be used when you already have shared_ptr<Buffer>
(aka ConstBufferPtr). The second parameter is not size (size is part of Buffer object), rather it is offset from the beginning. The use case for this method is that you have shared_ptr<Buffer>
, which holds multiple Blocks (either sequence or sub-TLVs). This method does not copy data, only copies shared_ptr
pointer.
- Status changed from New to Rejected
I didn't notice the different in second parameter.
The following snippet works well:
// g++ -std=c++0x x.cpp `pkg-config --cflags --libs libndn-cxx`
#include <ndn-cxx/encoding/block.hpp>
using ndn::Buffer;
using ndn::BufferPtr;
using ndn::Block;
int
main()
{
BufferPtr buffer = std::make_shared<Buffer>(4);
uint8_t* buf = buffer->get();
buf[0] = 0x01;
buf[1] = 0x02;
buf[2] = 0xcc;
buf[3] = 0xdd;
Block block1;
Block block2;
std::cout << Block::fromBuffer(buffer, 0, block1)
<< Block::fromBuffer(buf, 4, block2)
<< std::endl;
std::cout << block1.type() << "\t" << block1.size() << "\t" << block1.value_size() << std::endl;
std::cout << block2.type() << "\t" << block2.size() << "\t" << block2.value_size() << std::endl;
return 0;
}
Also available in: Atom
PDF