diff mbox

[OpenWrt-Devel,4/4] blobmsg_add_json_from_file(): fixed const-correctness with libjson

Message ID 1441782592-25267-4-git-send-email-dbachtin@init-ka.de
State Rejected
Headers show

Commit Message

Bachtin, Dmitri Sept. 9, 2015, 7:23 a.m. UTC
libjson's json_object_from_file takes a non-const char buffer as
its second parameter while libjson-c takes a const one. Use
strdup() to avoid using const data as non-const.

Signed-off-by: Dmitri Bachtin <dbachtin@init-ka.de>
---
 blobmsg_json.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)

Comments

Felix Fietkau Sept. 9, 2015, 8:17 a.m. UTC | #1
On 2015-09-09 09:23, Bachtin, Dmitri wrote:
> libjson's json_object_from_file takes a non-const char buffer as
> its second parameter while libjson-c takes a const one. Use
> strdup() to avoid using const data as non-const.
> 
> Signed-off-by: Dmitri Bachtin <dbachtin@init-ka.de>
> ---
>  blobmsg_json.c |    5 ++++-
>  1 files changed, 4 insertions(+), 1 deletions(-)
> 
> diff --git a/blobmsg_json.c b/blobmsg_json.c
> index ffde23d..de876cf 100644
> --- a/blobmsg_json.c
> +++ b/blobmsg_json.c
> @@ -97,7 +97,10 @@ out:
>  
>  bool blobmsg_add_json_from_file(struct blob_buf *b, const char *file)
>  {
> -	return __blobmsg_add_json(b, json_object_from_file(file));
> +	char* __file = strdup(file);
> +	bool result = __blobmsg_add_json(b, json_object_from_file(__file));
> +	free(__file);
> +	return result;
I think the non-const parameter of json_object_from_file is a bug, and
in that case, a cast is better than a completely unnecessary strdup().

- Felix
Bachtin, Dmitri Sept. 9, 2015, 9:30 a.m. UTC | #2
> I think the non-const parameter of json_object_from_file is a bug, and in that case, a cast is better than a completely unnecessary strdup().
> 
> - Felix

I've checked the source of json_object_from_file(char*) and the buffer walks directly into open(3) and vsyslog(3). open(3) takes a const buffer while vsyslog(3) takes a varargs list which doesn't tinker with the buffer, too.

You are completely right, Felix, and a cast is enough.
 -- Dmitri Bachtin
diff mbox

Patch

diff --git a/blobmsg_json.c b/blobmsg_json.c
index ffde23d..de876cf 100644
--- a/blobmsg_json.c
+++ b/blobmsg_json.c
@@ -97,7 +97,10 @@  out:
 
 bool blobmsg_add_json_from_file(struct blob_buf *b, const char *file)
 {
-	return __blobmsg_add_json(b, json_object_from_file(file));
+	char* __file = strdup(file);
+	bool result = __blobmsg_add_json(b, json_object_from_file(__file));
+	free(__file);
+	return result;
 }
 
 bool blobmsg_add_json_from_string(struct blob_buf *b, const char *str)