When the buffer is insufficient, the rest of the line is consumed (and lost). An error is indicated.
In C, a line of input is up to and including the
'\n
' C11 7.21.2 2. When the streams ends with something other than a new-line, how that is handled is implementation defined behavior. J.3.12. This code treats a'\n
' and end-of-file as the same. In both cases, a'\n'
is not include in the saved buffer.Allocating memory per external input can lead to abuse. This allows external forces to overwhelm memory allocation. The following does not use memory allocation like getline substitute that will enforce 'n' as limit of characters read or getline . Another alternative could use limited allocation, but that is not done here.
If code reads a
'0円'
, that is not practical to discern withfgets()
. This code returns the size of the spacedspace used indest
, which includes an appended null character.Lesser issues include
fgets()
handling ofNULL
augments, small buffer size, undefined buffer state onferror()
and use ofint
vssize_t
. The below code also clearly - I hope - handles that.An alternative allocate memory: Allocating memory per external input can lead to abuse. This allows external forces to overwhelm memory allocation. The following does not use memory allocation like getline substitute that will enforce 'n' as limit of characters read or getline . Another alternative could use limited allocation, but that is not done here.
Portability concerns: Might a common or rare case fail on some select systemsystems?
General comments. (on any code).
The code below is listed as one file for code review convenience, yet would usually would be is separatedseparate .ch, .hc files.
When the buffer is insufficient, the rest of the line is consumed (and lost). An error is indicated.
In C, a line of input is up to and including the
'\n
' C11 7.21.2 2. When the streams ends with something other than a new-line, how that is handled is implementation defined behavior. J.3.12. This code treats a'\n
' and end-of-file as the same. In both cases, a'\n'
is not include in the saved buffer.Allocating memory per external input can lead to abuse. This allows external forces to overwhelm memory allocation. The following does not use memory allocation like getline substitute that will enforce 'n' as limit of characters read or getline . Another alternative could use limited allocation, but that is not done here.
If code reads a
'0円'
, that is not practical to discern withfgets()
. This code returns the size of the spaced used indest
, which includes an appended null character.Lesser issues include
fgets()
handling ofNULL
augments, small buffer size, undefined buffer state onferror()
and use ofint
vssize_t
. The below code also clearly - I hope - handles that.
Portability concerns: Might a common or rare case fail on some select system?
General comments. (on any code)
The code below is listed as one for code review convenience, yet would usually be is separated .c, .h files.
When the buffer is insufficient, the rest of the line is consumed (and lost). An error is indicated.
In C, a line of input is up to and including the
'\n
' C11 7.21.2 2. When the streams ends with something other than a new-line, how that is handled is implementation defined behavior. J.3.12. This code treats a'\n
' and end-of-file as the same. In both cases, a'\n'
is not include in the saved buffer.If code reads a
'0円'
, that is not practical to discern withfgets()
. This code returns the size of the space used indest
, which includes an appended null character.Lesser issues include
fgets()
handling ofNULL
augments, small buffer size, undefined buffer state onferror()
and use ofint
vssize_t
. The below code also clearly - I hope - handles that.An alternative allocate memory: Allocating memory per external input can lead to abuse. This allows external forces to overwhelm memory allocation. The following does not use memory allocation like getline substitute that will enforce 'n' as limit of characters read or getline . Another alternative could use limited allocation, but that is not done here.
Portability concerns: Might a common or rare case fail on some select systems?
General comments (on any code).
The code below is listed as one file for code review convenience, yet would usually would be is separate .h, .c files.
Performance concerns are appreciated when they are backed with real measurements.
Performance concerns are appreciated when they are backed with real measurements.