yyjson_ptr_get_sint() returns false for positive integer
Closed this issue ยท 5 comments
Describe the bug
The yyjson_ptr_get_sint()
function returns false for positive integer values, unexpectedly. Demonstration code in following comment.
The same value can be access as expected with yyjson_ptr_get_uint()
, and negative values work as expected with yyjson_ptr_get_sint()
.
This was discovered on v0.8.0 and tested against the current master at f1867b0
Your environment
- OS: Ubuntu 20.04 x64, and macOS 14.2
- Compiler: gcc 11.4.0 on Ubuntu and clang 15.0.0 on macos
Demonstration code
//cc -Wall -I. yyjson.c yytest.c -o yytest
#include "yyjson.h"
#include "inttypes.h"
int
main ()
{
const char *jsonstr = "{\"posvalue\": 123, \"negvalue\": -123, \"strvalue\": \"hello\"}";
yyjson_doc *doc = NULL;
yyjson_val *root = NULL;
int64_t sintvalue = 0;
uint64_t uintvalue = 0;
const char *strvalue = NULL;
doc = yyjson_read (jsonstr, strlen(jsonstr), 0);
root = yyjson_doc_get_root(doc);
if (root) {
printf ("JSON string parsed\n");
if (yyjson_ptr_get_sint (root, "/posvalue", &sintvalue)) {
printf ("posvalue(sint): %"PRId64"\n", sintvalue);
}
else {
printf ("posvalue(sint) ERROR\n");
}
if (yyjson_ptr_get_uint (root, "/posvalue", &uintvalue)) {
printf ("posvalue(uint): %"PRIu64"\n", uintvalue);
}
else {
printf ("posvalue(uint) ERROR\n");
}
if (yyjson_ptr_get_sint (root, "/negvalue", &sintvalue)) {
printf ("negvalue(sint): %"PRId64"\n", sintvalue);
}
else {
printf ("negvalue(sint) ERROR\n");
}
if (yyjson_ptr_get_str (root, "/strvalue", &strvalue)) {
printf ("strvalue: %s\n", strvalue);
}
else {
printf ("strvalue ERROR\n");
}
yyjson_doc_free(doc);
}
return 0;
}
Output:
% cc -Wall -I. yyjson.c yytest.c -o yytest
% ./yytest
JSON string parsed
posvalue(sint) ERROR
posvalue(uint): 123
negvalue(sint): -123
strvalue: hello
The uint
and sint
are two different types here, and the function will return false if the types don't match:
Lines 7751 to 7755 in f1867b0
I think we should add auto-type conversion to this function, which would return true if the integer can be converted without overflowing.
I note that basic getters of values has a yyjson_get_int()
which appears to do the uint or sint functionality. (unfortunately it also truncates to int
so the full range of the [u]int64_t is lost). Maybe that approach would work for the ptr versions as well.
that's perfect, thanks @ibireme!