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_putnbr_fd] Test feedback seems incorrect or is worded wrongly. #137

Open
DanielAJL opened this issue Oct 31, 2022 · 2 comments
Open

Comments

@DanielAJL
Copy link

For the tests I am having an issue with the "our putnbr_fd allocate memory, wtf ???" test result. I am not allocating any memory as far as I can tell. Once I change int to long int for the li_num variable; it fixes both failed cases. I'm not sure if the description of the test result is correct or that the test itself is not correct.

Test results:
image

My ft_putnbr_fd:

void	ft_putnbr_fd(int n, int fd)
{
	int	li_num;

	li_num = n;
	if (li_num < 0)
	{
		write(fd, "-", 1);
		li_num *= -1;
	}
	if (li_num >= 10)
	{
		ft_putnbr_fd((li_num / 10), fd);
		ft_putchar_fd(li_num % 10 + '0', fd);
	}
	else
		ft_putchar_fd(li_num + '0', fd);
}
@github-actions
Copy link

Hello! Thanks for contributing to the libft unit test.

Note that this repository is not maintained by the owner anymore, instead there is a bot that will automatically merge any reviewed pull requests.
If you feel like it, here are some links that can help you submiting a change in the code base::

@lorenzoedoardofrancesco
Copy link

lorenzoedoardofrancesco commented Nov 8, 2022

You're overflowing your int when multiplying li_num *= -1 and li_num is MIN_INT (-2147483648)
It would give you +2147483648, which is MAX_INT + 1
That's the reason of the two errors (signed integer overflow is undefined behavior, which gives you error *2)

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

2 participants