From 75cec62c0c5a8f79eac39d30c6f748f3cb08f766 Mon Sep 17 00:00:00 2001 From: 82marbag <69267416+82marbag@users.noreply.github.com> Date: Tue, 6 Jul 2021 16:37:11 -0400 Subject: [PATCH] string: avoid panic strtok on null input The behavior of strtok when given a NULL input on the first call is to panic on an unmapped virtual address. This commit changes it by returning NULL instead of panic. Signed-off-by: Daniele Ahmed --- include/string.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/string.h b/include/string.h index 6aaeb5c4..eb36890e 100644 --- a/include/string.h +++ b/include/string.h @@ -288,10 +288,13 @@ static inline size_t strcspn(const char *s1, const char *s2) { static inline char *strtok(char *s, const char *delim) { - static char *lasts; + static char *lasts = NULL; int ch; - if (NULL == s) + if (s == NULL && lasts == NULL) + return NULL; + + if (s == NULL) s = lasts + 1; do {