Browse Source

[fix] libft: ft_str(n)cmp: check string are not NULL to avoid segfault.

Moul 4 years ago
parent
commit
d38eebacbf
2 changed files with 9 additions and 1 deletions
  1. 4 0
      src/ft_strcmp.c
  2. 5 1
      src/ft_strncmp.c

+ 4 - 0
src/ft_strcmp.c

@@ -17,6 +17,10 @@ int		ft_strcmp(const char *s1, const char *s2)
 	unsigned char s11;
 	unsigned char s22;
 
+	if (s1 == NULL && s2 == NULL)
+		return (0);
+	else if (s1 == NULL || s2 == NULL)
+		return (-122);
 	s11 = *s1;
 	s22 = *s2;
 	if (!s11 && !s22)

+ 5 - 1
src/ft_strncmp.c

@@ -6,7 +6,7 @@
 /*   By: mazimi <mazimi@student.42.fr>              +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2014/11/25 14:13:10 by mazimi            #+#    #+#             */
-/*   Updated: 2014/12/02 16:05:23 by mazimi           ###   ########.fr       */
+/*   Updated: 2016/09/30 15:24:22 by mazimi           ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -17,6 +17,10 @@ int		ft_strncmp(const char *s1, const char *s2, size_t n)
 	unsigned char s11;
 	unsigned char s22;
 
+	if (s1 == NULL && s2 == NULL)
+		return (0);
+	else if (s1 == NULL || s2 == NULL)
+		return (-122);
 	s11 = *s1;
 	s22 = *s2;
 	if (!n || (!s11 && !s22))