[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20130110233456.GB2470@blaptop>
Date: Fri, 11 Jan 2013 08:34:56 +0900
From: Minchan Kim <minchan@...nel.org>
To: Randy Dunlap <rdunlap@...radead.org>
Cc: Michal Nazarewicz <mina86@...a86.com>,
Andrew Morton <akpm@...ux-foundation.org>, linux-mm@...ck.org,
linux-kernel@...r.kernel.org, Mel Gorman <mgorman@...e.de>,
Andy Whitcroft <apw@...dowen.org>,
Alexander Nyberg <alexn@....su.se>
Subject: Re: [PATCH] Fix wrong EOF compare
Hi Randy,
On Thu, Jan 10, 2013 at 08:12:24AM -0800, Randy Dunlap wrote:
> On 01/10/13 07:26, Michal Nazarewicz wrote:
> > On Thu, Jan 10 2013, Minchan Kim <minchan@...nel.org> wrote:
> >> 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'
Oops. It was typo when I cooked the patch after testing in my ARM system.
> and test build it please.
Keep in mind. Thanks!
>
> >
> > 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.
> >
> >> 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 <mgorman@...e.de>
> >> Cc: Andy Whitcroft <apw@...dowen.org>
> >> Cc: Alexander Nyberg <alexn@....su.se>
> >> Signed-off-by: Minchan Kim <minchan@...nel.org>
> >> ---
> >> Documentation/page_owner.c | 6 ++++--
> >> 1 file changed, 4 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/Documentation/page_owner.c b/Documentation/page_owner.c
> >> index f0156e1..b777fb6 100644
> >> --- a/Documentation/page_owner.c
> >> +++ b/Documentation/page_owner.c
> >> @@ -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;
> >
> >
> >
>
>
> --
> ~Randy
>
> --
> To unsubscribe, send a message with 'unsubscribe linux-mm' in
> the body to majordomo@...ck.org. For more info on Linux MM,
> see: http://www.linux-mm.org/ .
> Don't email: <a href=mailto:"dont@...ck.org"> email@...ck.org </a>
--
Kind regards,
Minchan Kim
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists