From e9fcb9b332c5510fd174e50604cd321c3cae6b3a Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Thu, 1 Sep 2022 16:57:54 +0200 Subject: [PATCH] Prevent crash in gettok() and similar functions by checking for NULL input. Return NULL instead. --- src/misc/string.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/misc/string.c b/src/misc/string.c index 580cc0167..5a06b9599 100644 --- a/src/misc/string.c +++ b/src/misc/string.c @@ -393,6 +393,9 @@ gettok(char **s) int paren; const char *token, *token_e; + if (!*s) + return NULL; + paren = 0; *s = skip_ws(*s); @@ -599,6 +602,10 @@ void findtok_noparen(char **p_str, char **p_token, char **p_token_end) char *gettok_noparens(char **s) { char *token, *token_e; + + if (!*s) + return NULL; + findtok_noparen(s, &token, &token_e); if (token == (char *) NULL) { return (char *) NULL; /* return NULL if we come to end of line */ @@ -654,6 +661,10 @@ void findtok_np(char** p_str, char** p_token, char** p_token_end) char* gettok_np(char** s) { char* token, * token_e; + + if (!*s) + return NULL; + findtok_np(s, &token, &token_e); if (token == (char*)NULL) { return (char*)NULL; /* return NULL if we come to end of line */ @@ -673,6 +684,9 @@ gettok_model(char **s) char c; const char *token, *token_e; + if (!*s) + return NULL; + *s = skip_ws(*s); if (!**s) @@ -706,6 +720,9 @@ gettok_instance(char **s) char c; const char *token, *token_e; + if (!*s) + return NULL; + *s = skip_ws(*s); if (!**s) @@ -740,6 +757,9 @@ gettok_char(char **s, char p, bool inc_p, bool nested) char c; const char *token, *token_e; + if (!*s) + return NULL; + *s = skip_ws(*s); if (!**s)