From c495547a70767d5efa3dc6855e6ee983c08b204f Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Mon, 21 Jul 2025 17:26:14 +0200 Subject: [PATCH] fix: redid all the checks for the color - checks if it has less or more than 3 fields - checks if it's only digits - checks if it's between 0 and 255 --- src/map/setters.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/map/setters.c b/src/map/setters.c index 5c476c0..d2c2ca0 100644 --- a/src/map/setters.c +++ b/src/map/setters.c @@ -13,9 +13,35 @@ #include "../../libft/includes/libft.h" #include "../utils/frees.h" +bool perform_color_checks(int *color, char *strcolor, t_mapdata *map) +{ + size_t i; + int colorvalue; + + i = 0; + while (i < ft_strlen(strcolor) - 1) + { + if (!ft_isdigit(strcolor[i])) + { + ft_strlcpy(map->error, "invalid characters in color definition", 1024); + return (false); + } + i++; + } + colorvalue = ft_atoi(strcolor); + if (colorvalue < 0 || colorvalue > 255) + { + ft_strlcpy(map->error, "invalid value for colors", 1024); + return (false); + } + *color = colorvalue; + return (true); +} + unsigned long set_color(const char *s, t_mapdata *map) { int i; + bool isok; unsigned long finalcolor; char **tab; int rgb[100]; @@ -26,15 +52,15 @@ unsigned long set_color(const char *s, t_mapdata *map) { if (i > 2) ft_strlcpy(map->error, "too many colors", 16); - rgb[i] = ft_atoi(tab[i]); + isok = perform_color_checks(&rgb[i], tab[i], map); free(tab[i]); i++; } free(tab); - if (rgb[0] < 0 || rgb[0] > 255 - || rgb[1] < 0 || rgb[1] > 255 - || rgb[2] < 0 || rgb[2] > 255) - return (ft_strlcpy(map->error, "Invalid color", 14), 0); + if (i <= 2) + return (ft_strlcpy(map->error, "Not enough colors", 18), 0); + if (!isok) + return (0); finalcolor = ((rgb[0] & 0xff) << 16) + ((rgb[1] & 0xff) << 8) + (rgb[2] & 0xff); return (finalcolor);