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

yyjson_is_sint() returns false for positive integers #182

Open
wridgers opened this issue Sep 25, 2024 · 2 comments
Open

yyjson_is_sint() returns false for positive integers #182

wridgers opened this issue Sep 25, 2024 · 2 comments

Comments

@wridgers
Copy link

Describe the bug
yyjson_is_sint() returns false for positive integers

Your environment

  • OS: Gentoo
  • Compiler: gcc 13.3.1

Additional context
I assume this is related to #152.

I wrote a few quick tests:

     json = "123";
     doc = yyjson_read(json, strlen(json), 0);
     val = yyjson_doc_get_root(doc);
     yy_assert(validate_val_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_UINT));
     yy_assert(strcmp(yyjson_get_type_desc(val), "uint") == 0);
     yy_assert(yyjson_get_uint(val) == (u64)123);
+    yy_assert(yyjson_is_sint(val));
     yy_assert(yyjson_get_sint(val) == (i64)123);
     yy_assert(yyjson_get_int(val) == (i64)123);
     yy_assert(yyjson_get_real(val) == (f64)0);
     yy_assert(yyjson_get_num(val) == (f64)123);
     yy_assert(yyjson_get_bool(val) == false);
     yyjson_doc_free(doc);
     json = "-123";
     doc = yyjson_read(json, strlen(json), 0);
     val = yyjson_doc_get_root(doc);
     yy_assert(validate_val_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_SINT));
     yy_assert(strcmp(yyjson_get_type_desc(val), "sint") == 0);
     yy_assert(yyjson_get_uint(val) == (u64)-123);
+    yy_assert(yyjson_is_sint(val));
     yy_assert(yyjson_get_sint(val) == (i64)-123);
     yy_assert(yyjson_get_int(val) == (i64)-123);
     yy_assert(yyjson_get_real(val) == (f64)0);
     yy_assert(yyjson_get_num(val) == (f64)-123);
     yyjson_doc_free(doc);

The 123 variant fails, while the -123 variant passes.

@ibireme
Copy link
Owner

ibireme commented Sep 25, 2024

Positive integers are read as uint type, and negative integers are read as sint type. So is_sint(uint_val) returns false.
I recommend using yyjson_is_int(val) instead, which returns true for both uint and sint types.

@Paril
Copy link

Paril commented Jan 27, 2025

I just ran into this foot-gun; it's kinda strange, although your explanation makes sense it is extremely unintuitive from a C perspective. 9223372036854775807 is not sint, is uint but is also int - however get_int implicitly down-casts it to int32. I think what tripped me up was that the API is not symmetrical with the mut API.

I don't know how open you are to API changes, but honestly I think the integer stuff (at least on the immutable side) needs a bit of a re-work or at least fixes to the documentation, because nobody that reads it or the source of the getters/is_ functions will understand that "sint" does not actually mean what we think of as a signed integer, it means negative integer.

I ended up just replacing the integral API entirely with ones that return fixed types and ensure they are in range of their respective types, so that the end user does not need to think about the distinction (ie, yyjson_is_int64 will always be true if the integer stored in the val is representable is int64, and yyjson_get_int16 will always return an int16 but will clamp it to the representable range just in case somebody calls it without type-checking first; I guess you could use an arg to determine whether you want it to overflow or wrap).

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

3 participants