Skip to contentSkip to conversation

ipaddress: Fix segfault in 'addr showdump'

PATCH In review Phil Sutter 3 participants 3 messages Sep 13, 2017 – Sep 21, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ipaddress: Fix segfault in 'addr showdump'

Obviously, 'addr showdump' feature wasn't adjusted to json output support. As a consequence, calls to print_string() in print_addrinfo() tried to dereference a NULL FILE pointer.

Cc Julien Fortin <[email protected]>
Fixes d0e720111aad2 ("ip: ipaddress.c: add support for json output")

--
Changes since v1:
Align json output with that of 'ip -j addr show':
- Interface index label is 'ifindex', not 'index' and it doesn't belong to 'addr_info' array.
- Create one 'addr_info' array per dumped address, not one for all.

---
 ip/ipaddress.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
ip/ipaddress.c +18 -2
index 9797145023966..4c47809570410 100644
@@ -1801,17 +1801,33 @@ static int show_handler(const struct sockaddr_nl *nl,
 {
 	struct ifaddrmsg *ifa = NLMSG_DATA(n);
 
-	printf("if%d:\n", ifa->ifa_index);
+	open_json_object(NULL);
+	print_int(PRINT_ANY, "ifindex", "if%d:\n", ifa->ifa_index);
+
+	open_json_array(PRINT_JSON, "addr_info");
+	open_json_object(NULL);
+
 	print_addrinfo(NULL, n, stdout);
+
+	close_json_object();
+	close_json_array(PRINT_JSON, NULL);
+
+	close_json_object();
 	return 0;
 }
 
 static int ipaddr_showdump(void)
 {
+	int err;
+
 	if (ipadd_dump_check_magic())
 		exit(-1);
 
-	exit(rtnl_from_file(stdin, &show_handler, NULL));
+	new_json_obj(json, stdout);
+	err = rtnl_from_file(stdin, &show_handler, NULL);
+	delete_json_obj();
+
+	exit(err);
 }
 
 static int restore_handler(const struct sockaddr_nl *nl,
Signature · 1 line
2.13.1
Acked-by on v2 1/1
#1 lore raw

v2 looks good to me, thanks for catching this segfault.

Quoted 7 lines · Phil Sutter

Obviously, 'addr showdump' feature wasn't adjusted to json output support. As a consequence, calls to print_string() in print_addrinfo() tried to dereference a NULL FILE pointer.

Cc Julien Fortin <[email protected]>
Fixes d0e720111aad2 ("ip: ipaddress.c: add support for json output")
Quoted 53 lines

--
Changes since v1:
Align json output with that of 'ip -j addr show':
- Interface index label is 'ifindex', not 'index' and it doesn't belong to 'addr_info' array.
- Create one 'addr_info' array per dumped address, not one for all.

---
 ip/ipaddress.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
ip/ipaddress.c +18 -3
index 9797145023966..4c47809570410 100644
@@ -1801,17 +1801,33 @@ static int show_handler(const struct sockaddr_nl *nl,
 {
        struct ifaddrmsg *ifa = NLMSG_DATA(n);
 
-       printf("if%d:\n", ifa->ifa_index);
+       open_json_object(NULL);
+       print_int(PRINT_ANY, "ifindex", "if%d:\n", ifa->ifa_index);
+
+       open_json_array(PRINT_JSON, "addr_info");
+       open_json_object(NULL);
+
        print_addrinfo(NULL, n, stdout);
+
+       close_json_object();
+       close_json_array(PRINT_JSON, NULL);
+
+       close_json_object();
        return 0;
 }
 
 static int ipaddr_showdump(void)
 {
+       int err;
+
        if (ipadd_dump_check_magic())
                exit(-1);
 
-       exit(rtnl_from_file(stdin, &show_handler, NULL));
+       new_json_obj(json, stdout);
+       err = rtnl_from_file(stdin, &show_handler, NULL);
+       delete_json_obj();
+
+       exit(err);
 }
 
 static int restore_handler(const struct sockaddr_nl *nl,
--

2.13.1

#2 lore raw
In reply to Phil Sutter
Quoted 13 lines · On Wed, 13 Sep 2017 11:20:34 +0200 Phil Sutter <[email protected]> wrote:

Obviously, 'addr showdump' feature wasn't adjusted to json output support. As a consequence, calls to print_string() in print_addrinfo() tried to dereference a NULL FILE pointer.

Cc Julien Fortin <[email protected]>
Fixes d0e720111aad2 ("ip: ipaddress.c: add support for json output")

--
Changes since v1:
Align json output with that of 'ip -j addr show':
- Interface index label is 'ifindex', not 'index' and it doesn't belong to 'addr_info' array.
- Create one 'addr_info' array per dumped address, not one for all.

Quoted 4 lines
---
 ip/ipaddress.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

Looks good, applied.