Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ft_atoi_base condition should exclude max_digit #67

Closed
adshz opened this issue May 27, 2024 · 3 comments
Closed

ft_atoi_base condition should exclude max_digit #67

adshz opened this issue May 27, 2024 · 3 comments

Comments

@adshz
Copy link

adshz commented May 27, 2024

  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.

@pasqualerossi
Copy link
Owner

create a pull request and I'll accept the request.

@C42joseri
Copy link

Had the same kind of problem with 'f' being accepted in base 15.
In theory we should only be able to go up to 'e' (being 14 in base 10). The code does 'e' and '10' correctly, showing 14 and 15 respectively, the problem is, it also shows 15 for 'f'.
When going through the code at first, I noticed something strange with base 16. The 'else if' condition in line 17 excluded 'g' as a character although the 'c <= max_digit' would have included it.
Everything else seems OKAY! I'm still fairly new to 42 and coding in general so please bear with me ^^
Also, thank you for your github! I don't know what I'd do with out it . You're a REAL G :) :)

@pasqualerossi
Copy link
Owner

create a pull request and I'll update it. Thanks!

amema42 added a commit to amema42/42-School-Exam-Rank-02 that referenced this issue Aug 1, 2024
longer but:
from issue pasqualerossi#67 : "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. "


we can fix the in 2 ways I think, adding (-1) or excluding max_digit in the condition... I just added -1 to let the exercise be kinda clear...idk:

in any case this is another version of int get_digit (char c, int digits_in_base);
------------------------
int get_digit(char c, int digits_in_base)
{
	if (digits_in_base <= 10)
	{
		if (c >= '0' && c < '0' + digits_in_base)
			return (c - '0');
	}
	else
	{
		if (c >= '0' && c <= '9')
			return (c - '0');
		else if (c >= 'a' && c < 'a' + digits_in_base - 10)
			return (10 + c - 'a');
	}
	return (-1);
}
-------------------------



& also:
1. fixed the main loop condition: the condition while ((digit = get_digit(to_lower(*str), str_base)) >= 0) directly gets the digit and breaks the loop if it's invalid - this ensures that we handle characters properly
2. added checks 4 Input Validation:
- Checks if the str is NULL.
- Checks if str_base is within the valid range (2 to 16) - according to subject :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants