Skip to content

Commit

Permalink
util/json: Use json_object_get_uint64() with uint64 support
Browse files Browse the repository at this point in the history
If HAVE_JSON_U64=1, utils/json.c:display_hex() can call
json_object_get_int64() on a struct json_object created with
json_object_new_uint64(). In the context of 'ndctl list --regions
--human', this results in a static value of 0x7fffffffffffffff being
displayed for iset_id, as seen in #217.

Correct hex values are observed with the use of
json_object_get_uint64(). To support builds against older json-c, use a
new static inline function util_json_get_u64() to fallback to
json_object_get_int64() if HAVE_JSON_U64=0.

Link: #217
Fixes: 691cd24 ("json: Add support for json_object_new_uint64()")
Signed-off-by: Justin Ernst <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Vishal Verma <[email protected]>
  • Loading branch information
justinrernst authored and stellarhopper committed Mar 1, 2024
1 parent a37665a commit 731ca1f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion util/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static int display_size(struct json_object *jobj, struct printbuf *pbuf,
static int display_hex(struct json_object *jobj, struct printbuf *pbuf,
int level, int flags)
{
unsigned long long val = json_object_get_int64(jobj);
unsigned long long val = util_json_get_u64(jobj);
static char buf[32];

snprintf(buf, sizeof(buf), "\"%#llx\"", val);
Expand Down
12 changes: 12 additions & 0 deletions util/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,22 @@ static inline struct json_object *util_json_new_u64(unsigned long long val)
{
return json_object_new_uint64(val);
}

static inline unsigned long long util_json_get_u64(struct json_object *jobj)
{
return json_object_get_uint64(jobj);
}

#else /* fallback to signed */
static inline struct json_object *util_json_new_u64(unsigned long long val)
{
return json_object_new_int64(val);
}

static inline unsigned long long util_json_get_u64(struct json_object *jobj)
{
return json_object_get_int64(jobj);
}

#endif /* HAVE_JSON_U64 */
#endif /* __UTIL_JSON_H__ */

0 comments on commit 731ca1f

Please sign in to comment.