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

Silent error with overflowed integer #41

Open
skull591 opened this issue Aug 1, 2023 · 1 comment
Open

Silent error with overflowed integer #41

skull591 opened this issue Aug 1, 2023 · 1 comment

Comments

@skull591
Copy link

skull591 commented Aug 1, 2023

Hello,

Thanks for this great library. While using it, I encounterd an error with overflowed integer. I was able to reproduce this on json-data.cpp with a large integer.

* limitations under the License.
*/

#include <iostream>
#include "xpack/json.h"
#include <string>

using namespace std;

struct Example {
    string type;
    xpack::JsonData data;
    XPACK(O(type, data));
};

struct Range {
    int min;
    int max;
    XPACK(O(min, max));
};

struct User {
    int id;
    string name;
    XPACK(O(id, name));
};

static void test(const std::string &data) {
    Example e;
    xpack::json::decode(data, e);
    if (e.type == "range") {
        Range r;
        e.data.Get(r);
    } else if (e.type == "user") {
	User u;
	e.data.Get(u);
	cout << u.id << endl;
    } else {

    }

}

int main(int argc, char *argv[]) {
    (void)argc;
    (void)argv;

    string s1 = "{\"type\":\"range\", \"data\":{\"min\":12, \"max\":33}}";
    string s2 = "{\"type\":\"user\", \"data\":{\"id\":123344343434343435, \"name\":\"xpack\"}}";
    test(s1);
    test(s2);
    
    return 0;
}

With a large number in the id of s2, no error was reported (I tried with a larger number and an error indicting not integer is given, which I think should be the correct behavior), but the output is -545967093.

I'm using the latest version of the library on Ubuntu 20.04LTS.

@xyz347
Copy link
Owner

xyz347 commented Aug 1, 2023

Thanks for your feedback.
All integers are currently parsed as int64 and then static cast to the target type.
Let me think about how to deal with this better.

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

2 participants