From a71bf70ad1908b2aca8d46a58b9b7bcf71622858 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Sun, 21 Jul 2024 18:57:01 +0200 Subject: [PATCH] Harden the ft_strchr and ft_strrchr functions This makes them return NULL when the haystack is NULL. --- ft_str/ft_strchr.c | 4 +++- ft_str/ft_strrchr.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ft_str/ft_strchr.c b/ft_str/ft_strchr.c index 6a4e3b6..182001e 100644 --- a/ft_str/ft_strchr.c +++ b/ft_str/ft_strchr.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 10:41:10 by ljiriste #+# #+# */ -/* Updated: 2023/12/09 15:44:56 by ljiriste ### ########.fr */ +/* Updated: 2024/07/21 19:02:33 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,8 @@ char *ft_strchr(const char *s, int c) { + if (!s) + return (NULL); while (*s) { if (*s == (char)c) diff --git a/ft_str/ft_strrchr.c b/ft_str/ft_strrchr.c index 1afc2c1..8aef14e 100644 --- a/ft_str/ft_strrchr.c +++ b/ft_str/ft_strrchr.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 10:46:09 by ljiriste #+# #+# */ -/* Updated: 2023/12/09 15:24:36 by ljiriste ### ########.fr */ +/* Updated: 2024/07/21 18:41:05 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ char *ft_strrchr(const char *s, int c) { char *result; + if (!s) + return (NULL); result = NULL; while (*s) { -- 2.30.2