Skip to contentSkip to conversation

json_print: add NULL check before jsonw_string_field() in print_string()

PATCH Applied [email protected] 2 participants 2 messages May 18, 2025 – Jun 10, 2025

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

PATCH v2 1/1 json_print: add NULL check before jsonw_string_field() in print_string()

From: AntonMoryakov <[email protected]>

Static analyzer (Svace) reported a potential null pointer dereference in print_string(). Specifically, when both 'key' and 'value' are NULL, the function falls through to jsonw_string_field(_jw, key, value), which dereferences both pointers.

Although comments suggest this case is unlikely, it is safer to explicitly guard against it. This patch adds a check to ensure both key and value are non-NULL before passing to jsonw_string_field().

This resolves:
DEREF_AFTER_NULL: json_print.c:142

Found by Svace static analysis tool.

---
 json_print.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
json_print.c +4 -2
index 4f62767..76e654b 100644
@@ -138,13 +138,15 @@ void print_string(enum output_type type,
 			jsonw_name(_jw, key);
 		else if (!key && value)
 			jsonw_string(_jw, value);
-		else
+		else if (key && value)
 			jsonw_string_field(_jw, key, value);
 	} else if (_IS_FP_CONTEXT(type)) {
-		fprintf(stdout, fmt, value);
+		if (value)
+			fprintf(stdout, fmt, value);
 	}
 }
 
+
 /*
  * value's type is bool. When using this function in FP context you can't pass
  * a value to it, you will need to use "is_json_context()" to have different
Signature · 1 line
2.34.1
#1 lore raw
This patch was applied to ethtool/ethtool.git (master) by Michal Kubecek <[email protected]>:

Hello:

This patch was applied to ethtool/ethtool.git (master) by Michal Kubecek <[email protected]>:

Quoted 8 lines · On Sun, 18 May 2025 16:01:11 +0300 you wrote:

From: AntonMoryakov <[email protected]>

Static analyzer (Svace) reported a potential null pointer dereference in print_string(). Specifically, when both 'key' and 'value' are NULL, the function falls through to jsonw_string_field(_jw, key, value), which dereferences both pointers.

[...]

Here is the summary with links:
- [ethtool,V2] json_print: add NULL check before jsonw_string_field() in print_string()
https://git.kernel.org/pub/scm/network/ethtool/ethtool.git/commit/?id=fd328ccb3cc0

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html