Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request feature: Exemple Usage + possible bug: strings above 55 bytes not handle properly #3

Open
datgrog opened this issue Jul 25, 2019 · 7 comments

Comments

@datgrog
Copy link
Contributor

datgrog commented Jul 25, 2019

I'm playing around your rlpvalue package in order to serialize an ethereum transaction.

  • The possible bug is related to the "Lorem ipsum..." exemple
RLPValue lorem("Lorem ipsum dolor sit amet, consectetur adipisicing elit");
std::string serialized_lorem = lorem.write();

for (size_t i = 0; i < lorem.size(); i++) {
    printf("\\x%.2x ", lorem.c_str()[i]);
}

It prints \xffffffb8 \x4c \x6f ... but should print \xb8 \x38 \x4c ... instead, it looks like rlpvalue does not handle properly strings with length above 55 bytes, see medium post (Ctrl+F and "Rule 2")

  • Also, how does your package handle integer serialization ?

NB: On my mac, I also had to install brew install argp-standalone before doing the installation to compile properly

@xiaofo09
Copy link

xiaofo09 commented Oct 1, 2019

Did you solve that problem ? I have same problem in the latest version including your fix.

@datgrog
Copy link
Contributor Author

datgrog commented Oct 1, 2019

Did you solve that problem ? I have same problem in the latest version including your fix.

What input did you try to serialize ? What result do you expect from it ?

@xiaofo09
Copy link

xiaofo09 commented Oct 1, 2019

Same input like you. below:

RLPValue lorem("Lorem ipsum dolor sit amet, consectetur adipisicing elit");
std::string serialized_lorem = lorem.write();

for (size_t i = 0; i < serialized_lorem.size(); i++) {
    printf("\\x%.2x ", serialized_lorem.c_str()[i]);
}

But it prints out \xffffffb8 \x38 \x4c ... but should print \xb8 \x38 \x4c ...

@xiaofo09
Copy link

xiaofo09 commented Oct 1, 2019

My code is here.

/* g++ -o rlp rlp.cpp -I. -L/usr/local/lib -lrlpvalue -static */

#include <stdio.h>
#include "rlpvalue.h"

using namespace std;

int main(void)
{
    RLPValue lorem("Lorem ipsum dolor sit amet, consectetur adipisicing elit");
    std::string serialized_lorem = lorem.write();

    for (size_t i = 0; i < serialized_lorem.size(); i++) {
        printf("\\x%.2x ", serialized_lorem.c_str()[i]);
    }
    printf("\n");

    return 0;
}

@xiaofo09
Copy link

xiaofo09 commented Oct 1, 2019

I think the data is placed in "serialized_lorem" correctly.
Is this simply a matter of output?

@jgarzik
Copy link
Contributor

jgarzik commented Oct 1, 2019

Recommend using ParseHex and HexStr for going to/from hexidecimal. Example:
https://github.com/bloq/rlpvalue/blob/master/test/unitester.cpp#L129

			std::string genHex = HexStr(genOutput.begin(),
						    genOutput.end());
			fprintf(stderr, "INS :%s\nGENS:%s\n",
				outs.c_str(),
				genHex.c_str());

@darcys22
Copy link

darcys22 commented Jul 7, 2023

Pretty late to the party, this got me also. It isn't a bug in the serialized_lorem. Instead its an issue with how you print to hex.

In the code the length is prepended to the data using this:

static std::string encodeLength(size_t n, unsigned char offset)
{
	std::string rs;

	if (n < 56) {
		unsigned char ch = n + offset;
		rs.assign((const char *) &ch, 1);
	}

	else {
		// assert(n too big);
		std::string binlen = encodeBinary(n);

		unsigned char ch = binlen.size() + offset + 55;
		rs.assign((const char *) &ch, 1);
		rs.append(binlen);
	}

	return rs;
}

which represents the length as a single char, if the offset (0xC0 for example) pushes this above what can be represented as a single signed byte then it will be represented as a negative in std::string.

When printing if this gets interpreted as a signed char and converted to an unsigned char, then your output will be incorrect. Hence why HexStr is recommended going to hexidecimal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants