Skip to content

Commit

Permalink
refac: handle when invalid real number given
Browse files Browse the repository at this point in the history
  • Loading branch information
takumihara committed May 17, 2022
1 parent e522e72 commit b116df8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
7 changes: 7 additions & 0 deletions scenes_bonus/err_real_invalid.rt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
A 0.1 255,255,255
L -5,5,-5 1 255,255,255
C 0,0,-10 0.3,-0.1,1 30
# C 0,10,0 0,-1,-0 100

sp 6,0.5,1 2.0 176,176,176
ch 10a 10 255,255,255 0,0,255
7 changes: 7 additions & 0 deletions scenes_bonus/err_real_overflow.rt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
A 0.1 255,255,255
L -5,5,-5 1 255,255,255
C 0,0,-10 0.3,-0.1,1 30
# C 0,10,0 0,-1,-0 100

sp 6,0.5,1 2.0 176,176,176
ch 1000000000000000000000000000000000 50 255,255,255 0,0,255
9 changes: 4 additions & 5 deletions src/rt_params.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "../include/scene.h"
#include "../libft/libft.h"

char *load_ambient(t_program *p, char **info)
{
Expand Down Expand Up @@ -98,12 +97,12 @@ char *load_checker(t_program *p, char **info)
if (object->material.flag == (1 << MFLAG_CHECKER))
return (ERR_DUPLICATE_MATERIAL);
object->material.flag |= (1 << MFLAG_CHECKER);
object->material.checker_width = ft_atoi(info[1]);
if (!(0 <= object->material.checker_width))
if (!(atoi_strict(info[0], &object->material.checker_width) && 0 <= object->material.checker_width))
return (ERR_MISCONFIGURED_CHECKER);
object->material.checker_height = ft_atoi(info[1]);
if (!(0 <= object->material.checker_height))
printf("%d\n", object->material.checker_width);
if (!(atoi_strict(info[1], &object->material.checker_height) && 0 <= object->material.checker_height))
return (ERR_MISCONFIGURED_CHECKER);
printf("%d\n", object->material.checker_height);
if (!(get_color_from_str(info[2], &c) && check_color_range(c, 0.0, 255.0)))
return (ERR_MISCONFIGURED_CHECKER);
object->material.checker_col1 = color_mult(c, (double)1/255);
Expand Down
3 changes: 2 additions & 1 deletion src/scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ static void read_rt_file(char *filename, t_program *p)
if (err != NO_ERR)
break ;
}
err = check_lack_of_identifier(ident_flag);
if (err == NO_ERR)
err = check_lack_of_identifier(ident_flag);
free(line);
x_close(fd);
if (err != NO_ERR)
Expand Down
17 changes: 9 additions & 8 deletions libft/ft_atoi.c → utils/atoi_strict.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <limits.h>
#include <stdbool.h>

int ft_atoi(char *str)
bool atoi_strict(char *str, int *dst)
{
long long rtn;
int sign;
Expand All @@ -13,16 +13,17 @@ int ft_atoi(char *str)
sign = -1;
if (*str == '+' || *str == '-')
str++;
if (*str == '\0')
return (false);
while ('0' <= *str && *str <= '9')
{
if ((rtn * 10 + *str - '0') / 10 != rtn)
{
if (sign == 1)
return ((int)(LONG_MAX));
return ((int)(LONG_MIN));
}
return (false);
rtn = rtn * 10 + *str - '0';
str++;
}
return ((int)(sign * rtn));
*dst = (int)(sign * rtn);
if (*str == '\0')
return (true);
return (false);
}
1 change: 1 addition & 0 deletions utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ void exit_with_error_message(const char *msg);
bool ft_strtod(const char *s, double *val);
size_t count_2d_array(void **array);
void free_2d_array(void ***array);
bool atoi_strict(char *str, int *dst);

#endif

0 comments on commit b116df8

Please sign in to comment.