pasqualerossi/42-School-Exam-Rank-04

SEGFAULT when "./microshell cd"

arthurdemurger-vinci opened this issue · 2 comments

You have to add a protection in the write_error to avoid a segfault when string or argv are NULL.

How would you implement this protection? What would the code look like?

You could add that protection by simply adding a condition to both while loops.

// current code
int	write_error(char *string, char *argv)
{
	while (*string)
		write(2, string++, 1);
	while (*argv)
		write(2, argv++, 1);
	write(2, "\n", 1);
	return (1);
}

// protected code
int	write_error(char *string, char *argv)
{
	while (string && *string)
		write(2, string++, 1);
	while (argv && *argv)
		write(2, argv++, 1);
	write(2, "\n", 1);
	return (1);
}

the added string and argv in the while loop have to come first in the argument list of the while so that it is checked first and if either string or argv is NULL, it won't go into the loop and won't cause a SEGFAULT