Fix wrong EOF compare
Subsystems (2)
PATCH 1/1 Fix wrong EOF compare
getc returns "int" so EOF could be -1 but storing getc's return value to char directly makes the vaule to 255 so below condition is always false.
It happens in my ARM system so loop is not ended, then segfaulted. This patch fixes it.
*curr = getc(fin); // *curr = 255
if (*curr == EOF) return -1; // if ( 255 == -1)--- Documentation/page_owner.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
Documentation/page_owner.c +4 -2
@@ -32,12 +32,14 @@ int read_block(char *buf, FILE *fin) {int ret =0 ;int hit =0 ; +int vaule;char *curr = buf;for (;;) { - *curr =getc (fin); -if (*curr ==EOF )return -1 ; + value =getc (fin); +if (value ==EOF )return -1 ; + *curr = value; ret++;if (*curr =='\n' && hit ==1 )return ret -1 ;
Signature · 7 lines
1.7.9.5 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 3 lines · Minchan Kim
getc returns "int" so EOF could be -1 but storing getc's return value to char directly makes the vaule to 255 so below condition is always false.
Technically, this is implementation defined and I believe on many systems char is signed thus the loop will end on EOF or byte 255.
Either way, my point is the patch is correct, but the comment is not. ;)
Of course, even better if the function just used fgets(), ie. something like:
int read_block(char *buf, int buf_size, FILE *fin)
{
char *curr = buf, *const buf_end = buf + buf_size;
while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) {
if (*curr == '\n') /* empty line */
return curr - buf;
curr += strlen(curr);
}
return -1; /* EOF or no space left in buf. */
}which is much shorter and does not have buffer overflow issues.
Quoted 35 lines
It happens in my ARM system so loop is not ended, then segfaulted. This patch fixes it.
*curr = getc(fin); // *curr = 255 if (*curr == EOF) return -1; // if ( 255 == -1)Cc Mel Gorman <[email protected]>Cc Andy Whitcroft <[email protected]>Cc Alexander Nyberg <[email protected]>Signed-off-by Minchan Kim <[email protected]>--- Documentation/page_owner.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)Documentation/page_owner.c +4
-2@@ -32,12 +32,14 @@ int read_block(char *buf, FILE *fin) {int ret =0 ;int hit =0 ; +int vaule;char *curr = buf;for (;;) { - *curr =getc (fin); -if (*curr ==EOF )return -1 ; + value =getc (fin); +if (value ==EOF )return -1 ; + *curr = value; ret++;if (*curr =='\n' && hit ==1 )return ret -1 ;
Signature · 4 lines
Best regards, _ _ .o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michał “mina86” Nazarewicz (o o) ooo +----<email/xmpp: [email protected]>--------------ooO--(_)--Ooo--
Randy Dunlap
reviewerQuoted 9 lines · Michal Nazarewicz
Quoted 3 lines · Minchan Kim
getc returns "int" so EOF could be -1 but storing getc's return value to char directly makes the vaule to 255 so below condition is always false.
Technically, this is implementation defined and I believe on many systems char is signed thus the loop will end on EOF or byte 255.
Either way, my point is the patch is correct, but the comment is not. ;)
and change spelling of 'vaule' to 'value'
and test build it please.
Quoted 57 lines
Of course, even better if the function just used
fgets(), ie. something like:int
read_block(char *buf, int buf_size, FILE *fin){ char *curr = buf, *const buf_end = buf + buf_size; while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) { if (*curr == '\n') /* empty line */ return curr - buf; curr += strlen(curr); } return -1; /* EOF or no space left in buf. */ }which is much shorter and does not have buffer overflow issues.
Quoted 35 lines
It happens in my ARM system so loop is not ended, then segfaulted. This patch fixes it.
*curr = getc(fin); // *curr = 255 if (*curr == EOF) return -1; // if ( 255 == -1)Cc Mel Gorman <[email protected]>Cc Andy Whitcroft <[email protected]>Cc Alexander Nyberg <[email protected]>Signed-off-by Minchan Kim <[email protected]>--- Documentation/page_owner.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)Documentation/page_owner.c +4
-2@@ -32,12 +32,14 @@ int read_block(char *buf, FILE *fin) {int ret =0 ;int hit =0 ; +int vaule;char *curr = buf;for (;;) { - *curr =getc (fin); -if (*curr ==EOF )return -1 ; + value =getc (fin); +if (value ==EOF )return -1 ; + *curr = value; ret++;if (*curr =='\n' && hit ==1 )return ret -1 ;
Signature · 7 lines
~Randy -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Hi Randy,
Quoted 6 lines · Randy Dunlap
Quoted 5 lines · Michal Nazarewicz
Quoted 3 lines · Minchan Kim
getc returns "int" so EOF could be -1 but storing getc's return value to char directly makes the vaule to 255 so below condition is always false.
Quoted 6 lines
Quoted 4 lines
Technically, this is implementation defined and I believe on many systems char is signed thus the loop will end on EOF or byte 255.
Either way, my point is the patch is correct, but the comment is not. ;)
and change spelling of 'vaule' to 'value'
Oops. It was typo when I cooked the patch after testing in my ARM system.
Quoted 1 line
and test build it please.
Keep in mind. Thanks!
Quoted 68 lines
Quoted 58 lines
Of course, even better if the function just used
fgets(), ie. something like:int
read_block(char *buf, int buf_size, FILE *fin){ char *curr = buf, *const buf_end = buf + buf_size; while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) { if (*curr == '\n') /* empty line */ return curr - buf; curr += strlen(curr); } return -1; /* EOF or no space left in buf. */ }which is much shorter and does not have buffer overflow issues.
Quoted 35 lines
It happens in my ARM system so loop is not ended, then segfaulted. This patch fixes it.
*curr = getc(fin); // *curr = 255 if (*curr == EOF) return -1; // if ( 255 == -1)Cc Mel Gorman <[email protected]>Cc Andy Whitcroft <[email protected]>Cc Alexander Nyberg <[email protected]>Signed-off-by Minchan Kim <[email protected]>--- Documentation/page_owner.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)Documentation/page_owner.c +4
-2@@ -32,12 +32,14 @@ int read_block(char *buf, FILE *fin) {int ret =0 ;int hit =0 ; +int vaule;char *curr = buf;for (;;) { - *curr =getc (fin); -if (*curr ==EOF )return -1 ; + value =getc (fin); +if (value ==EOF )return -1 ; + *curr = value; ret++;if (*curr =='\n' && hit ==1 )return ret -1 ;Signature · 7 lines
~Randy -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Signature · 8 lines
Kind regards, Minchan Kim -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Hello Michal,
Quoted 7 lines · Michal Nazarewicz
Quoted 3 lines · Minchan Kim
getc returns "int" so EOF could be -1 but storing getc's return value to char directly makes the vaule to 255 so below condition is always false.
Technically, this is implementation defined and I believe on many systems char is signed thus the loop will end on EOF or byte 255.
True. That's why there is no problem in x86.
The problem would happens on ARM and PowerPC which defined char as unsigned in GCC.
Quoted 2 lines
Either way, my point is the patch is correct, but the comment is not. ;)
Yeb. It was not elegant :(
How about this?
The C standards allows the character type char to be singed or unsinged, depending on the platform and compiler. Most of systems uses signed char, but those based on PowerPC and ARM processors typically use unsigned char. This can lead to unexpected results when the variable is used to compare with EOF(-1).
This patch fixes the problem.
Quoted 17 lines
Of course, even better if the function just used
fgets(), ie. something like:int
read_block(char *buf, int buf_size, FILE *fin){ char *curr = buf, *const buf_end = buf + buf_size; while (buf_end - curr > 1 && fgets(curr, buf_end - curr, fin)) { if (*curr == '\n') /* empty line */ return curr - buf; curr += strlen(curr); } return -1; /* EOF or no space left in buf. */ }
Quoted 1 line
which is much shorter and does not have buffer overflow issues.
Looks better. It is bug fix + code clean up + performance enhance. Although it's very straightforward, I would like to separate simple bug fix with others so I will resend two patches.
Thanks for the review!
Quoted 42 lines
Quoted 36 lines
It happens in my ARM system so loop is not ended, then segfaulted. This patch fixes it.
*curr = getc(fin); // *curr = 255 if (*curr == EOF) return -1; // if ( 255 == -1)Cc Mel Gorman <[email protected]>Cc Andy Whitcroft <[email protected]>Cc Alexander Nyberg <[email protected]>Signed-off-by Minchan Kim <[email protected]>--- Documentation/page_owner.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)Documentation/page_owner.c +4
-2@@ -32,12 +32,14 @@ int read_block(char *buf, FILE *fin) {int ret =0 ;int hit =0 ; +int vaule;char *curr = buf;for (;;) { - *curr =getc (fin); -if (*curr ==EOF )return -1 ; + value =getc (fin); +if (value ==EOF )return -1 ; + *curr = value; ret++;if (*curr =='\n' && hit ==1 )return ret -1 ;Signature · 4 lines
Best regards, _ _ .o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o ..o | Computer Science, MichaA? a??mina86a?? Nazarewicz (o o) ooo +----<email/xmpp: [email protected]>--------------ooO--(_)--Ooo--
Signature · 8 lines
Kind regards, Minchan Kim -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>