Last active
June 20, 2024 14:09
-
-
Save AdrianSkar/a8ab63c6c06bf56f5bea67b0f91a8116 to your computer and use it in GitHub Desktop.
[GNL]: main for printf segfault on EOF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's because of
gccwhen compiling and how it behaves with%s+NULL. Doesn't happen on eitherccorclang.