Harden the ft_strchr and ft_strrchr functions
authorLukas Jiriste <ljiriste@student.42prague.com>
Sun, 21 Jul 2024 16:57:01 +0000 (18:57 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 21 Jul 2024 17:02:47 +0000 (19:02 +0200)
This makes them return NULL when the haystack is NULL.

ft_str/ft_strchr.c
ft_str/ft_strrchr.c

index 6a4e3b6a483347fa2ceb1bad1bdf1e29c57f93a0..182001e95575f7c064be39992363cd1010bcdbb8 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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)
index 1afc2c1712587e6dbf372389d25af1ae8f1d3692..8aef14e248ea021ac2267be1708c6c3e756d2805 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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)
        {