From 31dcfead33c3ec2eb4a4e90dc7bb3a327291c839 Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Sat, 22 Aug 2020 13:27:36 +0200 Subject: [PATCH] Easier 64 bit safety in json_object_to_fd Avoid any form of conversion issues between data types by using the data types required by write(2) directly. --- json_util.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/json_util.c b/json_util.c index a065523..f60f274 100644 --- a/json_util.c +++ b/json_util.c @@ -185,9 +185,9 @@ int json_object_to_fd(int fd, struct json_object *obj, int flags) } static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename) { - int ret; + ssize_t ret; const char *json_str; - unsigned int wpos, wsize; + size_t wpos, wsize; filename = filename ? filename : "(fd)"; @@ -196,8 +196,7 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const return -1; } - /* CAW: probably unnecessary, but the most 64bit safe */ - wsize = (unsigned int)(strlen(json_str) & UINT_MAX); + wsize = strlen(json_str); wpos = 0; while (wpos < wsize) { @@ -209,7 +208,7 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const } /* because of the above check for ret < 0, we can safely cast and add */ - wpos += (unsigned int)ret; + wpos += (size_t)ret; } return 0;