Commit 7b8a9e18 authored by Pisit Sawangvonganan's avatar Pisit Sawangvonganan Committed by Fabio Baltieri
Browse files

net: shell: ensure the shell `sh` is valid before call shell_printf



It is possible that the `sh` was not set before use.
This change adds a NULL check for `sh` in the following macros:
PR, PR_SHELL, PR_ERROR, PR_INFO, and PR_WARNING.
In case `sh` is NULL, the above macros will call `printk` instead.

Fixes #68793

Signed-off-by: default avatarPisit Sawangvonganan <pisit@ndrsolution.com>
parent f4c8020d
Loading
Loading
Loading
Loading
+40 −10
Original line number Diff line number Diff line
@@ -9,19 +9,49 @@
#include <zephyr/net/net_ip.h>

#define PR(fmt, ...)                                                            \
	shell_fprintf(sh, SHELL_NORMAL, fmt, ##__VA_ARGS__)
	do {                                                                    \
		if (sh) {                                                       \
			shell_fprintf(sh, SHELL_NORMAL, fmt, ##__VA_ARGS__);    \
		} else {                                                        \
			printk(fmt, ##__VA_ARGS__);                             \
		}                                                               \
	} while (false)

#define PR_SHELL(sh, fmt, ...)                                                  \
	shell_fprintf(sh, SHELL_NORMAL, fmt, ##__VA_ARGS__)
	do {                                                                    \
		if (sh) {                                                       \
			shell_fprintf(sh, SHELL_NORMAL, fmt, ##__VA_ARGS__);    \
		} else {                                                        \
			printk(fmt, ##__VA_ARGS__);                             \
		}                                                               \
	} while (false)

#define PR_ERROR(fmt, ...)                                                      \
	shell_fprintf(sh, SHELL_ERROR, fmt, ##__VA_ARGS__)
	do {                                                                    \
		if (sh) {                                                       \
			shell_fprintf(sh, SHELL_ERROR, fmt, ##__VA_ARGS__);     \
		} else {                                                        \
			printk(fmt, ##__VA_ARGS__);                             \
		}                                                               \
	} while (false)

#define PR_INFO(fmt, ...)                                                       \
	shell_fprintf(sh, SHELL_INFO, fmt, ##__VA_ARGS__)
	do {                                                                    \
		if (sh) {                                                       \
			shell_fprintf(sh, SHELL_INFO, fmt, ##__VA_ARGS__);      \
		} else {                                                        \
			printk(fmt, ##__VA_ARGS__);                             \
		}                                                               \
	} while (false)

#define PR_WARNING(fmt, ...)                                                    \
	shell_fprintf(sh, SHELL_WARNING, fmt, ##__VA_ARGS__)
	do {                                                                    \
		if (sh) {                                                       \
			shell_fprintf(sh, SHELL_WARNING, fmt, ##__VA_ARGS__);   \
		} else {                                                        \
			printk(fmt, ##__VA_ARGS__);                             \
		}                                                               \
	} while (false)

#include "net_private.h"
#include "../ip/ipv6.h"