Really it depends how you want to handle lines that are too long.
fgets
with a decent sized buffer will work generally, and you can detect that it has "failed" - the buffer end has no newline char. It is possible to avoid always doing a strlen() to confirm if the buffer is overflowed, but that is a different question.
Perhaps your strategy is to simply skip lines that can't be processed, or perhaps the rest of the line is just a comment you would ignore anyway, in which case, it is easy to then put fgets
in a loop to discard the rest of the line with no allocation penalty.
If you do want to read the whole line regardless then getline
may be the better strategy for you. The malicious user would need a lot of disk space to cause the bad behaviour you describe, or perhaps pass /dev/random or similar as the input filename.
Again, if getline
can't realloc it will fail in a way that you can recover from, though if you are reusing the buffer for multiple line reads, you might want to free the buffer that it does have after an error before trying to read more, as it is still allocated and may have grown as large as it could before failing.