42-School-Exam-Rank-02 icon indicating copy to clipboard operation
42-School-Exam-Rank-02 copied to clipboard

ft_atoi_base condition should exclude max_digit

Open adshz opened this issue 9 months ago • 3 comments

  8 int get_digit(char c, int digits_in_base)
  9 {
 10         int max_digit;
 11         if (digits_in_base <= 10)
 12                 max_digit = digits_in_base + '0';
 13         else
 14                 max_digit = digits_in_base - 10 + 'a';
 15         if (c >= '0' && c <= '9' && c <= max_digit)
 16                 return (c - '0');
 17         else if (c >= 'a' && c <= 'f' && c <= max_digit)
 18                 return (10 + c - 'a');
 19         else
 20                 return (-1);
 21 }

In the line 15 and line 17, the condition is wrong. In any base $b$, the digits must be in the range of 0 to $b-1$. For example, the valid digits are 0 and 1 if we are talking about base 2. So the condition should exceed the maximum allowable digits.

adshz avatar May 27 '24 15:05 adshz