From: Ben Pfaff Date: Sun, 9 Mar 2014 23:01:25 +0000 (-0700) Subject: line-reader: Fix bad math when size_t is bigger than off_t. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bc2bcb0e2e77b4b02a32d6179b8ee6673bfdffa6;p=pspp line-reader: Fix bad math when size_t is bigger than off_t. When size_t is bigger than off_t, pos - r->length will end up very large, instead of negative, when r->length > pos, so the MAX didn't help to get rid of negative values. I'm not sure that this should really happen in practice, but it's better to avoid it. Reported by GCC on x86-64. --- diff --git a/src/libpspp/line-reader.c b/src/libpspp/line-reader.c index 6f90b502cd..56a368da29 100644 --- a/src/libpspp/line-reader.c +++ b/src/libpspp/line-reader.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2010, 2011 Free Software Foundation, Inc. + Copyright (C) 2010, 2011, 2014 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -311,9 +311,9 @@ off_t line_reader_tell (const struct line_reader *r) { off_t pos = lseek (r->fd, 0, SEEK_CUR); - if (pos >= 0) - pos = MAX (0, pos - r->length); - return pos; + return (pos < 0 ? pos + : pos >= r->length ? pos - r->length + : 0); } /* Returns true if end of file has been encountered reading R. */