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
This commit is contained in:
Theo Champion 2025-07-21 17:26:14 +02:00
parent ea8d3e085a
commit c495547a70

View file

@ -13,9 +13,35 @@
#include "../../libft/includes/libft.h" #include "../../libft/includes/libft.h"
#include "../utils/frees.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) unsigned long set_color(const char *s, t_mapdata *map)
{ {
int i; int i;
bool isok;
unsigned long finalcolor; unsigned long finalcolor;
char **tab; char **tab;
int rgb[100]; int rgb[100];
@ -26,15 +52,15 @@ unsigned long set_color(const char *s, t_mapdata *map)
{ {
if (i > 2) if (i > 2)
ft_strlcpy(map->error, "too many colors", 16); 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]); free(tab[i]);
i++; i++;
} }
free(tab); free(tab);
if (rgb[0] < 0 || rgb[0] > 255 if (i <= 2)
|| rgb[1] < 0 || rgb[1] > 255 return (ft_strlcpy(map->error, "Not enough colors", 18), 0);
|| rgb[2] < 0 || rgb[2] > 255) if (!isok)
return (ft_strlcpy(map->error, "Invalid color", 14), 0); return (0);
finalcolor = ((rgb[0] & 0xff) << 16) finalcolor = ((rgb[0] & 0xff) << 16)
+ ((rgb[1] & 0xff) << 8) + (rgb[2] & 0xff); + ((rgb[1] & 0xff) << 8) + (rgb[2] & 0xff);
return (finalcolor); return (finalcolor);