commit:     beb68b28d4694aa576d0e06fc37deb31d83cef80
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Fri Mar 23 11:55:26 2018 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Fri Mar 23 11:55:26 2018 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=beb68b28

getline: fix comparison of integers of different signs

Just use an int to store getline return, then ensure it is positive,
such that a cast to size_t is guaranteed to result in the same value.

 libq/profile.c |  7 ++++---
 qcache.c       | 14 ++++++++------
 qlop.c         | 17 ++++++++++-------
 qmerge.c       | 14 ++++++++------
 qsearch.c      | 12 +++++++-----
 quse.c         | 29 ++++++++++++++++++-----------
 6 files changed, 55 insertions(+), 38 deletions(-)

diff --git a/libq/profile.c b/libq/profile.c
index c954ba6..ad631a1 100644
--- a/libq/profile.c
+++ b/libq/profile.c
@@ -6,7 +6,8 @@ q_profile_walk_at(int dir_fd, const char *dir, const char *file,
 {
        FILE *fp;
        int subdir_fd, fd;
-       size_t buflen, linelen;
+       int linelen;
+       size_t buflen;
        char *buf;
 
        /* Pop open this profile dir */
@@ -46,10 +47,10 @@ q_profile_walk_at(int dir_fd, const char *dir, const char 
*file,
        }
 
        buf = NULL;
-       while ((linelen = getline(&buf, &buflen, fp)) != -1) {
+       while ((linelen = getline(&buf, &buflen, fp)) >= 0) {
                char *s;
 
-               rmspace_len(buf, linelen);
+               rmspace_len(buf, (size_t)linelen);
 
                s = strchr(buf, '#');
                if (s)

diff --git a/qcache.c b/qcache.c
index 7cd100e..0082e5b 100644
--- a/qcache.c
+++ b/qcache.c
@@ -220,7 +220,8 @@ qcache_read_cache_file(const char *filename)
        char *buf;
        FILE *f;
        portage_cache *ret = NULL;
-       size_t len, buflen, linelen;
+       int linelen;
+       size_t len, buflen;
 
        if ((f = fopen(filename, "r")) == NULL)
                goto err;
@@ -234,8 +235,8 @@ qcache_read_cache_file(const char *filename)
        len = sizeof(*ret) + s.st_size + 1;
        ret = xzalloc(len);
 
-       while ((linelen = getline(&buf, &buflen, f)) != -1) {
-               rmspace_len(buf, linelen);
+       while ((linelen = getline(&buf, &buflen, f)) >= 0) {
+               rmspace_len(buf, (size_t)linelen);
 
                if (strncmp(buf, "DEPEND=", 7) == 0)
                        ret->DEPEND = xstrdup(buf + 7);
@@ -868,7 +869,8 @@ qcache_load_arches(const char *overlay)
 {
        FILE *fp;
        char *filename, *s;
-       size_t buflen, linelen;
+       int linelen;
+       size_t buflen;
        char *buf;
 
        xasprintf(&filename, "%s/profiles/arch.list", overlay);
@@ -879,8 +881,8 @@ qcache_load_arches(const char *overlay)
        archlist_count = 0;
        arch_longest_len = 0;
        buf = NULL;
-       while ((linelen = getline(&buf, &buflen, fp)) != -1) {
-               rmspace_len(buf, linelen);
+       while ((linelen = getline(&buf, &buflen, fp)) >= 0) {
+               rmspace_len(buf, (size_t)linelen);
 
                if ((s = strchr(buf, '#')) != NULL)
                        *s = '\0';

diff --git a/qlop.c b/qlop.c
index f0c35b7..5be2ddd 100644
--- a/qlop.c
+++ b/qlop.c
@@ -232,7 +232,8 @@ show_emerge_history(int listflag, array_t *atoms, const 
char *logfile,
                     time_t start_time, time_t end_time)
 {
        FILE *fp;
-       size_t buflen, linelen;
+       int linelen;
+       size_t buflen;
        char *buf, merged;
        char *p, *q;
        bool showit;
@@ -246,16 +247,17 @@ show_emerge_history(int listflag, array_t *atoms, const 
char *logfile,
        }
 
        buf = NULL;
-       while ((linelen = getline(&buf, &buflen, fp)) != -1) {
+       while ((linelen = getline(&buf, &buflen, fp)) >= 0) {
                if (linelen < 30)
                        continue;
 
-               rmspace_len(buf, linelen);
+               rmspace_len(buf, (size_t)linelen);
                if ((p = strchr(buf, ':')) == NULL)
                        continue;
                *p = 0;
                q = p + 3;
-               /* Make sure there's leading white space and not a truncated 
string. #573106 */
+               /* Make sure there's leading white space and not a truncated
+                * string. #573106 */
                if (p[1] != ' ' || p[2] != ' ')
                        continue;
 
@@ -331,7 +333,8 @@ static void
 show_sync_history(const char *logfile, time_t start_time, time_t end_time)
 {
        FILE *fp;
-       size_t buflen, linelen;
+       int linelen;
+       size_t buflen;
        char *buf, *p;
        time_t t;
 
@@ -342,7 +345,7 @@ show_sync_history(const char *logfile, time_t start_time, 
time_t end_time)
 
        buf = NULL;
        /* Just find the finish lines. */
-       while ((linelen = getline(&buf, &buflen, fp)) != -1) {
+       while ((linelen = getline(&buf, &buflen, fp)) >= 0) {
                /* This cuts out like ~10% of the log. */
                if (linelen < 35)
                        continue;
@@ -356,7 +359,7 @@ show_sync_history(const char *logfile, time_t start_time, 
time_t end_time)
                        continue;
                p += 19;
 
-               rmspace_len(buf, linelen);
+               rmspace_len(buf, (size_t)linelen);
 
                t = (time_t)atol(buf);
                if (t < start_time || t > end_time)

diff --git a/qmerge.c b/qmerge.c
index 1f75acc..960a9a4 100644
--- a/qmerge.c
+++ b/qmerge.c
@@ -1653,7 +1653,8 @@ static int
 parse_packages(queue *todo)
 {
        FILE *fp;
-       size_t buflen, linelen;
+       int linelen;
+       size_t buflen;
        char *buf, *p;
        struct pkg_t Pkg;
        depend_atom *pkg_atom;
@@ -1665,8 +1666,8 @@ parse_packages(queue *todo)
        repo[0] = '\0';
 
        /* First consume the header with the common data. */
-       while ((linelen = getline(&buf, &buflen, fp)) != -1) {
-               rmspace_len(buf, linelen);
+       while ((linelen = getline(&buf, &buflen, fp)) >= 0) {
+               rmspace_len(buf, (size_t)linelen);
                if (buf[0] == '\0')
                        break;
 
@@ -1785,7 +1786,8 @@ static queue *
 qmerge_add_set_file(const char *dir, const char *file, queue *set)
 {
        FILE *fp;
-       size_t buflen, linelen;
+       int linelen;
+       size_t buflen;
        char *buf, *fname;
 
        /* Find the file to read */
@@ -1800,8 +1802,8 @@ qmerge_add_set_file(const char *dir, const char *file, 
queue *set)
 
        /* Load each entry */
        buf = NULL;
-       while ((linelen = getline(&buf, &buflen, fp)) != -1) {
-               rmspace_len(buf, linelen);
+       while ((linelen = getline(&buf, &buflen, fp)) >= 0) {
+               rmspace_len(buf, (size_t)linelen);
                set = add_set(buf, set);
        }
        free(buf);

diff --git a/qsearch.c b/qsearch.c
index 1ebfccf..c2b2ebe 100644
--- a/qsearch.c
+++ b/qsearch.c
@@ -101,8 +101,9 @@ qsearch_ebuild_ebuild(int overlay_fd, const char *ebuild, 
const char *search_me,
        }
 
        char *buf = NULL;
-       size_t buflen, linelen;
-       while ((linelen = getline(&buf, &buflen, ebuildfp)) != -1) {
+       int linelen;
+       size_t buflen;
+       while ((linelen = getline(&buf, &buflen, ebuildfp)) >= 0) {
                if (linelen <= search_len)
                        continue;
                if (strncmp(buf, search_var, search_len) != 0)
@@ -194,10 +195,11 @@ int qsearch_main(int argc, char **argv)
                        continue;
                }
 
-               size_t buflen, linelen;
+               int linelen;
+               size_t buflen;
                char *buf = NULL;
-               while ((linelen = getline(&buf, &buflen, fp)) != -1) {
-                       rmspace_len(buf, linelen);
+               while ((linelen = getline(&buf, &buflen, fp)) >= 0) {
+                       rmspace_len(buf, (size_t)linelen);
                        if (!buf[0])
                                continue;
 

diff --git a/quse.c b/quse.c
index 81d99d8..ea8a326 100644
--- a/quse.c
+++ b/quse.c
@@ -82,7 +82,8 @@ static void
 quse_describe_flag(const char *overlay, unsigned int ind, unsigned int argc, 
char **argv)
 {
 #define NUM_SEARCH_FILES ARRAY_SIZE(search_files)
-       size_t buflen, linelen;
+       int linelen;
+       size_t buflen;
        char *buf, *p;
        unsigned int i, f;
        size_t s;
@@ -110,8 +111,8 @@ quse_describe_flag(const char *overlay, unsigned int ind, 
unsigned int argc, cha
                        if (fp[f] == NULL)
                                continue;
 
-                       while ((linelen = getline(&buf, &buflen, fp[f])) != -1) 
{
-                               rmspace_len(buf, linelen);
+                       while ((linelen = getline(&buf, &buflen, fp[f])) >= 0) {
+                               rmspace_len(buf, (size_t)linelen);
                                if (buf[0] == '#' || buf[0] == '\0')
                                        continue;
 
@@ -119,7 +120,9 @@ quse_describe_flag(const char *overlay, unsigned int ind, 
unsigned int argc, cha
                                        case 0: /* Global use.desc */
                                                if (!strncmp(buf, argv[i], s))
                                                        if (buf[s] == ' ' && 
buf[s + 1] == '-') {
-                                                               printf(" 
%sglobal%s:%s%s%s: %s\n", BOLD, NORM, BLUE, argv[i], NORM, buf + s + 3);
+                                                               printf(" 
%sglobal%s:%s%s%s: %s\n",
+                                                                               
BOLD, NORM, BLUE, argv[i], NORM,
+                                                                               
buf + s + 3);
                                                                goto skip_file;
                                                        }
                                                break;
@@ -131,14 +134,17 @@ quse_describe_flag(const char *overlay, unsigned int ind, 
unsigned int argc, cha
                                                if (!strncmp(p, argv[i], s)) {
                                                        if (p[s] == ' ' && p[s 
+ 1] == '-') {
                                                                *p = '\0';
-                                                               printf(" 
%slocal%s:%s%s%s:%s%s%s %s\n", BOLD, NORM, BLUE, argv[i], NORM, BOLD, buf, 
NORM, p + s + 3);
+                                                               printf(" 
%slocal%s:%s%s%s:%s%s%s %s\n",
+                                                                               
BOLD, NORM, BLUE, argv[i], NORM,
+                                                                               
BOLD, buf, NORM, p + s + 3);
                                                        }
                                                }
                                                break;
 
                                        case 2: /* Architectures arch.list */
                                                if (!strcmp(buf, argv[i])) {
-                                                       printf(" 
%sarch%s:%s%s%s: %s architecture\n", BOLD, NORM, BLUE, argv[i], NORM, argv[i]);
+                                                       printf(" 
%sarch%s:%s%s%s: %s architecture\n",
+                                                                       BOLD, 
NORM, BLUE, argv[i], NORM, argv[i]);
                                                        goto skip_file;
                                                }
                                                break;
@@ -191,8 +197,8 @@ quse_describe_flag(const char *overlay, unsigned int ind, 
unsigned int argc, cha
                /* Chop the trailing .desc for better display */
                *p = '\0';
 
-               while ((linelen = getline(&buf, &buflen, fp[0])) != -1) {
-                       rmspace_len(buf, linelen);
+               while ((linelen = getline(&buf, &buflen, fp[0])) >= 0) {
+                       rmspace_len(buf, (size_t)linelen);
                        if (buf[0] == '#' || buf[0] == '\0')
                                continue;
 
@@ -231,7 +237,8 @@ int quse_main(int argc, char **argv)
        char buf1[_Q_PATH_MAX];
        char buf2[_Q_PATH_MAX];
 
-       size_t ebuildlen, linelen;
+       int linelen;
+       size_t ebuildlen;
        char *ebuild;
 
        const char *search_var = NULL;
@@ -279,11 +286,11 @@ int quse_main(int argc, char **argv)
                int overlay_fd = open(overlay, O_RDONLY|O_CLOEXEC|O_PATH);
 
                ebuild = NULL;
-               while ((linelen = getline(&ebuild, &ebuildlen, fp)) != -1) {
+               while ((linelen = getline(&ebuild, &ebuildlen, fp)) >= 0) {
                        FILE *newfp;
                        int fd;
 
-                       rmspace_len(ebuild, linelen);
+                       rmspace_len(ebuild, (size_t)linelen);
 
                        fd = openat(overlay_fd, ebuild, O_RDONLY|O_CLOEXEC);
                        if (fd < 0)

Reply via email to