Skip to content

Commit b116df8

Browse files
committed
refac: handle when invalid real number given
1 parent e522e72 commit b116df8

File tree

6 files changed

+30
-14
lines changed

6 files changed

+30
-14
lines changed

scenes_bonus/err_real_invalid.rt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
A 0.1 255,255,255
2+
L -5,5,-5 1 255,255,255
3+
C 0,0,-10 0.3,-0.1,1 30
4+
# C 0,10,0 0,-1,-0 100
5+
6+
sp 6,0.5,1 2.0 176,176,176
7+
ch 10a 10 255,255,255 0,0,255

scenes_bonus/err_real_overflow.rt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
A 0.1 255,255,255
2+
L -5,5,-5 1 255,255,255
3+
C 0,0,-10 0.3,-0.1,1 30
4+
# C 0,10,0 0,-1,-0 100
5+
6+
sp 6,0.5,1 2.0 176,176,176
7+
ch 1000000000000000000000000000000000 50 255,255,255 0,0,255

src/rt_params.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "../include/scene.h"
2-
#include "../libft/libft.h"
32

43
char *load_ambient(t_program *p, char **info)
54
{
@@ -98,12 +97,12 @@ char *load_checker(t_program *p, char **info)
9897
if (object->material.flag == (1 << MFLAG_CHECKER))
9998
return (ERR_DUPLICATE_MATERIAL);
10099
object->material.flag |= (1 << MFLAG_CHECKER);
101-
object->material.checker_width = ft_atoi(info[1]);
102-
if (!(0 <= object->material.checker_width))
100+
if (!(atoi_strict(info[0], &object->material.checker_width) && 0 <= object->material.checker_width))
103101
return (ERR_MISCONFIGURED_CHECKER);
104-
object->material.checker_height = ft_atoi(info[1]);
105-
if (!(0 <= object->material.checker_height))
102+
printf("%d\n", object->material.checker_width);
103+
if (!(atoi_strict(info[1], &object->material.checker_height) && 0 <= object->material.checker_height))
106104
return (ERR_MISCONFIGURED_CHECKER);
105+
printf("%d\n", object->material.checker_height);
107106
if (!(get_color_from_str(info[2], &c) && check_color_range(c, 0.0, 255.0)))
108107
return (ERR_MISCONFIGURED_CHECKER);
109108
object->material.checker_col1 = color_mult(c, (double)1/255);

src/scene.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ static void read_rt_file(char *filename, t_program *p)
8989
if (err != NO_ERR)
9090
break ;
9191
}
92-
err = check_lack_of_identifier(ident_flag);
92+
if (err == NO_ERR)
93+
err = check_lack_of_identifier(ident_flag);
9394
free(line);
9495
x_close(fd);
9596
if (err != NO_ERR)
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include <limits.h>
1+
#include <stdbool.h>
22

3-
int ft_atoi(char *str)
3+
bool atoi_strict(char *str, int *dst)
44
{
55
long long rtn;
66
int sign;
@@ -13,16 +13,17 @@ int ft_atoi(char *str)
1313
sign = -1;
1414
if (*str == '+' || *str == '-')
1515
str++;
16+
if (*str == '\0')
17+
return (false);
1618
while ('0' <= *str && *str <= '9')
1719
{
1820
if ((rtn * 10 + *str - '0') / 10 != rtn)
19-
{
20-
if (sign == 1)
21-
return ((int)(LONG_MAX));
22-
return ((int)(LONG_MIN));
23-
}
21+
return (false);
2422
rtn = rtn * 10 + *str - '0';
2523
str++;
2624
}
27-
return ((int)(sign * rtn));
25+
*dst = (int)(sign * rtn);
26+
if (*str == '\0')
27+
return (true);
28+
return (false);
2829
}

utils/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ void exit_with_error_message(const char *msg);
1414
bool ft_strtod(const char *s, double *val);
1515
size_t count_2d_array(void **array);
1616
void free_2d_array(void ***array);
17+
bool atoi_strict(char *str, int *dst);
1718

1819
#endif

0 commit comments

Comments
 (0)