Skip to contentSkip to conversation

Fixing boolean value output for Netlink reported values in JSON format

PATCH In review Maxim Georgiev 2 participants 4 messages Dec 27, 2022 – Jan 9, 2023

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

PATCH 1/1 Fixing boolean value output for Netlink reported values in JSON format

Current implementation of show_bool_val() passes "val" parameter of pointer type as a last parameter to print_bool():
https://git.kernel.org/pub/scm/network/ethtool/ethtool.git/tree/netlink/netlink.h#n131
...
static inline void show_bool_val(const char *key, const char *fmt, uint8_t *val)

{
	if (is_json_context()) {
		if (val)
Quoted 1 line
			print_bool(PRINT_JSON, key, NULL, val);
	} else {

...
print_bool() expects the last parameter to be bool:
https://git.kernel.org/pub/scm/network/ethtool/ethtool.git/tree/json_print.c#n153
...
void print_bool(enum output_type type,
const char *key,
const char *fmt,
bool value)
{
...
Current show_bool_val() implementation converts "val" pointer to bool while calling show_bool_val(). As a result show_bool_val() always prints the value as "true" as long as it gets a non-null pointer to the boolean value, even if the referred boolean value is false.

Fixes 7e5c1ddbe67d ("pause: add --json support")
---
 netlink/netlink.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
netlink/netlink.h +1 -1
index 3240fca..1274a3b 100644
@@ -128,7 +128,7 @@ static inline void show_bool_val(const char *key, const char *fmt, uint8_t *val)
 {
 	if (is_json_context()) {
 		if (val)
-			print_bool(PRINT_JSON, key, NULL, val);
+			print_bool(PRINT_JSON, key, NULL, *val);
 	} else {
 		print_string(PRINT_FP, NULL, fmt, u8_to_bool(val));
 	}
Signature · 1 line
2.38.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Maxim Georgiev

Current implementation of show_bool_val() passes "val" parameter of pointer type as a last parameter to print_bool():
https://git.kernel.org/pub/scm/network/ethtool/ethtool.git/tree/netlink/netlink.h#n131
...
static inline void show_bool_val(const char *key, const char *fmt, uint8_t *val)

{
	if (is_json_context()) {
		if (val)
Quoted 1 line
			print_bool(PRINT_JSON, key, NULL, val);
	} else {

...
print_bool() expects the last parameter to be bool:
https://git.kernel.org/pub/scm/network/ethtool/ethtool.git/tree/json_print.c#n153
...
void print_bool(enum output_type type,
const char *key,
const char *fmt,
bool value)
{
...
Current show_bool_val() implementation converts "val" pointer to bool while calling show_bool_val(). As a result show_bool_val() always prints the value as "true" as long as it gets a non-null pointer to the boolean value, even if the referred boolean value is false.

Quoted 2 lines
Fixes 7e5c1ddbe67d ("pause: add --json support")

I'm assuming that val is never NULL :)

Quoted 20 lines
---
 netlink/netlink.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
netlink/netlink.h +1 -1
index 3240fca..1274a3b 100644
@@ -128,7 +128,7 @@ static inline void show_bool_val(const char *key, const char *fmt, uint8_t *val)
 {
 	if (is_json_context()) {
 		if (val)
-			print_bool(PRINT_JSON, key, NULL, val);
+			print_bool(PRINT_JSON, key, NULL, *val);
 	} else {
 		print_string(PRINT_FP, NULL, fmt, u8_to_bool(val));
 	}
Signature · 1 line
2.38.1
#2 lore raw
Quoted 27 lines · Simon Horman
Quoted 25 lines · Maxim Georgiev

Current implementation of show_bool_val() passes "val" parameter of pointer type as a last parameter to print_bool():
https://git.kernel.org/pub/scm/network/ethtool/ethtool.git/tree/netlink/netlink.h#n131
...
static inline void show_bool_val(const char *key, const char *fmt, uint8_t *val)

{
      if (is_json_context()) {
              if (val)
Quoted 1 line
                    print_bool(PRINT_JSON, key, NULL, val);
      } else {

...
print_bool() expects the last parameter to be bool:
https://git.kernel.org/pub/scm/network/ethtool/ethtool.git/tree/json_print.c#n153
...
void print_bool(enum output_type type,
const char *key,
const char *fmt,
bool value)
{
...
Current show_bool_val() implementation converts "val" pointer to bool while calling show_bool_val(). As a result show_bool_val() always prints the value as "true" as long as it gets a non-null pointer to the boolean value, even if the referred boolean value is false.

Quoted 6 lines
Quoted 2 lines
Fixes 7e5c1ddbe67d ("pause: add --json support")

I'm assuming that val is never NULL :)

Simon, thank you for taking a look!
Yes, the "if (val)" check on line 130 guarantees that "print_bool()" on line 131 is called only if val is not null.

Quoted 21 lines
Quoted 21 lines
---
 netlink/netlink.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
netlink/netlink.h +1 -2
index 3240fca..1274a3b 100644
@@ -128,7 +128,7 @@ static inline void show_bool_val(const char *key, const char *fmt, uint8_t *val)
 {
      if (is_json_context()) {
              if (val)
-                     print_bool(PRINT_JSON, key, NULL, val);
+                     print_bool(PRINT_JSON, key, NULL, *val);
      } else {
              print_string(PRINT_FP, NULL, fmt, u8_to_bool(val));
      }
--

2.38.1

#3 lore raw
Quoted 31 lines · Max Georgiev
Quoted 30 lines · Simon Horman
Quoted 27 lines · Maxim Georgiev

Current implementation of show_bool_val() passes "val" parameter of pointer type as a last parameter to print_bool():
https://git.kernel.org/pub/scm/network/ethtool/ethtool.git/tree/netlink/netlink.h#n131
...
static inline void show_bool_val(const char *key, const char *fmt, uint8_t *val)

{
      if (is_json_context()) {
              if (val)
Quoted 1 line
                    print_bool(PRINT_JSON, key, NULL, val);
      } else {

...
print_bool() expects the last parameter to be bool:
https://git.kernel.org/pub/scm/network/ethtool/ethtool.git/tree/json_print.c#n153
...
void print_bool(enum output_type type,
const char *key,
const char *fmt,
bool value)
{
...
Current show_bool_val() implementation converts "val" pointer to bool while calling show_bool_val(). As a result show_bool_val() always prints the value as "true" as long as it gets a non-null pointer to the boolean value, even if the referred boolean value is false.

Fixes 7e5c1ddbe67d ("pause: add --json support")
Quoted 7 lines
Quoted 3 lines

I'm assuming that val is never NULL :)

Simon, thank you for taking a look!
Yes, the "if (val)" check on line 130 guarantees that "print_bool()" on line 131 is called only if val is not null.

Thanks, that is pretty obvious now you point it out. Looks good to me :)

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
---
 netlink/netlink.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
netlink/netlink.h +1 -2
index 3240fca..1274a3b 100644
@@ -128,7 +128,7 @@ static inline void show_bool_val(const char *key, const char *fmt, uint8_t *val)
 {
      if (is_json_context()) {
              if (val)
-                     print_bool(PRINT_JSON, key, NULL, val);
+                     print_bool(PRINT_JSON, key, NULL, *val);
      } else {
              print_string(PRINT_FP, NULL, fmt, u8_to_bool(val));
      }
--

2.38.1