diff mbox series

[OpenWrt-Devel,libubox,8/9] blobmsg_json: fix possible uninitialized struct member

Message ID 20191120115926.23272-9-ynezz@true.cz
State Superseded
Headers show
Series fixes, some unit tests and GitLab CI | expand

Commit Message

Petr Štetiar Nov. 20, 2019, 11:59 a.m. UTC
clang-10 analyzer reports following:

 blobmsg_json.c:285:2: warning: The expression is an uninitialized value. The computed value will also be garbage
         s->indent_level++;
         ^~~~~~~~~~~~~~~~~

Signed-off-by: Petr Štetiar <ynezz@true.cz>
---
 blobmsg_json.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Yousong Zhou Nov. 20, 2019, 2:03 p.m. UTC | #1
On Wed, 20 Nov 2019 at 20:01, Petr Štetiar <ynezz@true.cz> wrote:
>
> clang-10 analyzer reports following:
>
>  blobmsg_json.c:285:2: warning: The expression is an uninitialized value. The computed value will also be garbage
>          s->indent_level++;
>          ^~~~~~~~~~~~~~~~~

Add else branch to initialize it in setup_strbuf() should suffice.

                yousong

>
> Signed-off-by: Petr Štetiar <ynezz@true.cz>
> ---
>  blobmsg_json.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/blobmsg_json.c b/blobmsg_json.c
> index a5980e8a2b62..18592111b4dd 100644
> --- a/blobmsg_json.c
> +++ b/blobmsg_json.c
> @@ -315,7 +315,7 @@ static void setup_strbuf(struct strbuf *s, struct blob_attr *attr, blobmsg_json_
>
>  char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent)
>  {
> -       struct strbuf s;
> +       struct strbuf s = {0};
>         bool array;
>         char *ret;
>
> @@ -349,7 +349,7 @@ char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_jso
>
>  char *blobmsg_format_json_value_with_cb(struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent)
>  {
> -       struct strbuf s;
> +       struct strbuf s = {0};
>         char *ret;
>
>         setup_strbuf(&s, attr, cb, priv, indent);
>
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel@lists.openwrt.org
> https://lists.openwrt.org/mailman/listinfo/openwrt-devel
Petr Štetiar Nov. 20, 2019, 2:13 p.m. UTC | #2
Yousong Zhou <yszhou4tech@gmail.com> [2019-11-20 22:03:06]:

> On Wed, 20 Nov 2019 at 20:01, Petr Štetiar <ynezz@true.cz> wrote:
> >
> > clang-10 analyzer reports following:
> >
> >  blobmsg_json.c:285:2: warning: The expression is an uninitialized value. The computed value will also be garbage
> >          s->indent_level++;
> >          ^~~~~~~~~~~~~~~~~
> 
> Add else branch to initialize it in setup_strbuf() should suffice.

Of course, and I even initialy planned to fix it that way, but then decided to
just init the complete struct as it doesn't add another codepath.  So probably
just a matter of taste?

-- ynezz
diff mbox series

Patch

diff --git a/blobmsg_json.c b/blobmsg_json.c
index a5980e8a2b62..18592111b4dd 100644
--- a/blobmsg_json.c
+++ b/blobmsg_json.c
@@ -315,7 +315,7 @@  static void setup_strbuf(struct strbuf *s, struct blob_attr *attr, blobmsg_json_
 
 char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_json_format_t cb, void *priv, int indent)
 {
-	struct strbuf s;
+	struct strbuf s = {0};
 	bool array;
 	char *ret;
 
@@ -349,7 +349,7 @@  char *blobmsg_format_json_with_cb(struct blob_attr *attr, bool list, blobmsg_jso
 
 char *blobmsg_format_json_value_with_cb(struct blob_attr *attr, blobmsg_json_format_t cb, void *priv, int indent)
 {
-	struct strbuf s;
+	struct strbuf s = {0};
 	char *ret;
 
 	setup_strbuf(&s, attr, cb, priv, indent);