Skip to content

Instantly share code, notes, and snippets.

@AdrianSkar
Last active June 20, 2024 14:09
Show Gist options
  • Save AdrianSkar/a8ab63c6c06bf56f5bea67b0f91a8116 to your computer and use it in GitHub Desktop.
Save AdrianSkar/a8ab63c6c06bf56f5bea67b0f91a8116 to your computer and use it in GitHub Desktop.
[GNL]: main for printf segfault on EOF
#include "get_next_line.h"
#include <stdio.h> // printf
#include <fcntl.h> // open, O_RDONLY, O_WRONLY, O_RDWR,
int main(void)
{
int gnl_calls = 3, fd, i;
char *line;
fd = open("test.txt", O_RDONLY);
if (fd == -1)
{
perror("Error opening file");
return 1;
}
while(gnl_calls-- > 0)
{
line = get_next_line(fd);
printf("%s\n", line); //! printf segfaults if EOF is reached
printf(" %s\n", line); //* Doesn't segfault
printf("%s \n", line); //* Doesn't segfault
printf("%s\n ", line); //* Doesn't segfault
printf("%s\t", line); //* Doesn't segfault
free(line);
}
close(fd);
return (0);
}
@AdrianSkar
Copy link
Author

It's because of gcc when compiling and how it behaves with %s + NULL. Doesn't happen on either cc or clang.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment