This is an automated email from the git hooks/post-receive script.

guillem pushed a commit to branch main
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=5601b476929a896e1ab67e66e1192d5a90346e75

commit 5601b476929a896e1ab67e66e1192d5a90346e75
Author: Guillem Jover <[email protected]>
AuthorDate: Wed Jul 10 22:32:23 2024 +0200

    Check for < 0 instead of == -1 from syscall return values
    
    While checking for == -1 is correct for all these calls, as that is
    what the error condition is specified to return, this can confuse static
    analyzers where these might consider other negative return values as
    unhandled and emit bogus potential overflow or underflow conditions.
    
    Make these checks < 0, which is shorter, also correct, and should pacify
    static analyzers. We leave alone the nice() function because it can
    return other negative values as part of its interface definition.
    
    Changelog: internal
---
 dselect/method.cc           |  8 ++++----
 lib/dpkg/dbmodify.c         | 12 ++++++------
 lib/dpkg/fdio.c             |  4 ++--
 lib/dpkg/file.c             | 14 +++++++-------
 lib/dpkg/mustlib.c          |  4 ++--
 lib/dpkg/parse.c            |  4 ++--
 lib/dpkg/subproc.c          |  4 ++--
 lib/dpkg/t/t-subproc.c      |  6 +++---
 lib/dpkg/trigdeferred.c     |  4 ++--
 src/common/selinux.c        |  2 +-
 src/deb/build.c             |  4 ++--
 src/deb/info.c              |  2 +-
 utils/start-stop-daemon.c   | 12 ++++++------
 utils/update-alternatives.c | 10 +++++-----
 14 files changed, 45 insertions(+), 45 deletions(-)

diff --git a/dselect/method.cc b/dselect/method.cc
index 5e7972ffd..ed7e83470 100644
--- a/dselect/method.cc
+++ b/dselect/method.cc
@@ -75,7 +75,7 @@ static void cu_unlockmethod(int, void**) {
   if (methlockfd < 0)
     internerr("method lock fd is %d < 0", methlockfd);
   fl.l_type=F_UNLCK; fl.l_whence= SEEK_SET; fl.l_start=fl.l_len=0;
-  if (fcntl(methlockfd,F_SETLK,&fl) == -1)
+  if (fcntl(methlockfd, F_SETLK, &fl) < 0)
     sthfailed(_("cannot unlock access method area"));
 }
 
@@ -104,9 +104,9 @@ static enum urqresult lockmethod(void) {
   if (methodlockfile == nullptr)
     methodlockfile = dpkg_db_get_path(METHLOCKFILE);
 
-  if (methlockfd == -1) {
+  if (methlockfd < 0) {
     methlockfd= open(methodlockfile, O_RDWR|O_CREAT|O_TRUNC, 0660);
-    if (methlockfd == -1) {
+    if (methlockfd < 0) {
       if ((errno == EPERM) || (errno == EACCES)) {
         sthfailed(_("requested operation requires superuser privilege"));
         return urqr_fail;
@@ -116,7 +116,7 @@ static enum urqresult lockmethod(void) {
     }
   }
   fl.l_type=F_WRLCK; fl.l_whence=SEEK_SET; fl.l_start=fl.l_len=0;
-  if (fcntl(methlockfd,F_SETLK,&fl) == -1) {
+  if (fcntl(methlockfd, F_SETLK, &fl) < 0) {
     if (errno == EACCES || errno == EAGAIN) {
       sthfailed(_("the access method area is already locked"));
       return urqr_fail;
diff --git a/lib/dpkg/dbmodify.c b/lib/dpkg/dbmodify.c
index a272289cb..a88ab4ad2 100644
--- a/lib/dpkg/dbmodify.c
+++ b/lib/dpkg/dbmodify.c
@@ -85,7 +85,7 @@ static void cleanupdates(void) {
 
   updateslength= -1;
   cdn = scandir(updatesdir, &cdlist, &ulist_select, alphasort);
-  if (cdn == -1) {
+  if (cdn < 0) {
     if (errno == ENOENT) {
       if (cstatus >= msdbrw_write &&
           dir_make_path(updatesdir, 0755) < 0)
@@ -222,9 +222,9 @@ modstatdb_is_locked(void)
   int lockfd;
   bool locked;
 
-  if (dblockfd == -1) {
+  if (dblockfd < 0) {
     lockfd = open(lockfile, O_RDONLY);
-    if (lockfd == -1) {
+    if (lockfd < 0) {
       if (errno == ENOENT)
         return false;
       ohshite(_("unable to check lock file for dpkg database directory %s"),
@@ -238,7 +238,7 @@ modstatdb_is_locked(void)
 
   /* We only close the file if there was no lock open, otherwise we would
    * release the existing lock on close. */
-  if (dblockfd == -1)
+  if (dblockfd < 0)
     close(lockfd);
 
   return locked;
@@ -252,7 +252,7 @@ modstatdb_can_lock(void)
 
   if (getenv("DPKG_FRONTEND_LOCKED") == NULL) {
     frontendlockfd = open(frontendlockfile, O_RDWR | O_CREAT | O_TRUNC, 0660);
-    if (frontendlockfd == -1) {
+    if (frontendlockfd < 0) {
       if (errno == EACCES || errno == EPERM)
         return false;
       else
@@ -264,7 +264,7 @@ modstatdb_can_lock(void)
   }
 
   dblockfd = open(lockfile, O_RDWR | O_CREAT | O_TRUNC, 0660);
-  if (dblockfd == -1) {
+  if (dblockfd < 0) {
     if (errno == EACCES || errno == EPERM)
       return false;
     else
diff --git a/lib/dpkg/fdio.c b/lib/dpkg/fdio.c
index 831b0cf85..075fb41fa 100644
--- a/lib/dpkg/fdio.c
+++ b/lib/dpkg/fdio.c
@@ -42,7 +42,7 @@ fd_read(int fd, void *buf, size_t len)
                ssize_t n;
 
                n = read(fd, ptr + total, len);
-               if (n == -1) {
+               if (n < 0) {
                        if (errno == EINTR || errno == EAGAIN)
                                continue;
                        return total ? -total : n;
@@ -70,7 +70,7 @@ fd_write(int fd, const void *buf, size_t len)
                ssize_t n;
 
                n = write(fd, ptr + total, len);
-               if (n == -1) {
+               if (n < 0) {
                        if (errno == EINTR || errno == EAGAIN)
                                continue;
                        return total ? -total : n;
diff --git a/lib/dpkg/file.c b/lib/dpkg/file.c
index 2d94bc08e..8b316a38b 100644
--- a/lib/dpkg/file.c
+++ b/lib/dpkg/file.c
@@ -101,17 +101,17 @@ file_copy_perms(const char *src, const char *dst)
 {
        struct stat stab;
 
-       if (stat(src, &stab) == -1) {
+       if (stat(src, &stab) < 0) {
                if (errno == ENOENT)
                        return;
                ohshite(_("unable to stat source file '%.250s'"), src);
        }
 
-       if (chown(dst, stab.st_uid, stab.st_gid) == -1)
+       if (chown(dst, stab.st_uid, stab.st_gid) < 0)
                ohshite(_("unable to change ownership of target file '%.250s'"),
                        dst);
 
-       if (chmod(dst, (stab.st_mode & ~S_IFMT)) == -1)
+       if (chmod(dst, (stab.st_mode & ~S_IFMT)) < 0)
                ohshite(_("unable to set mode of target file '%.250s'"), dst);
 }
 
@@ -181,7 +181,7 @@ file_unlock(int lockfd, const char *lockfile, const char 
*lockdesc)
 
        file_lock_setup(&fl, F_UNLCK);
 
-       if (fcntl(lockfd, F_SETLK, &fl) == -1)
+       if (fcntl(lockfd, F_SETLK, &fl) < 0)
                ohshite(_("unable to unlock %s"), lockdesc);
 }
 
@@ -208,7 +208,7 @@ file_is_locked(int lockfd, const char *filename)
 
        file_lock_setup(&fl, F_WRLCK);
 
-       if (fcntl(lockfd, F_GETLK, &fl) == -1)
+       if (fcntl(lockfd, F_GETLK, &fl) < 0)
                ohshit(_("unable to check file '%s' lock status"), filename);
 
        if (fl.l_type == F_WRLCK && fl.l_pid != getpid())
@@ -242,7 +242,7 @@ file_lock(int *lockfd, enum file_lock_flags flags, const 
char *filename,
        else
                lock_cmd = F_SETLK;
 
-       if (fcntl(*lockfd, lock_cmd, &fl) == -1) {
+       if (fcntl(*lockfd, lock_cmd, &fl) < 0) {
                const char *warnmsg;
 
                if (errno != EACCES && errno != EAGAIN)
@@ -254,7 +254,7 @@ file_lock(int *lockfd, enum file_lock_flags flags, const 
char *filename,
                            "See 
<https://wiki.debian.org/Teams/Dpkg/FAQ#db-lock>.");
 
                file_lock_setup(&fl, F_WRLCK);
-               if (fcntl(*lockfd, F_GETLK, &fl) == -1)
+               if (fcntl(*lockfd, F_GETLK, &fl) < 0)
                        ohshit(_("%s was locked by another process\n%s"),
                               desc, warnmsg);
 
diff --git a/lib/dpkg/mustlib.c b/lib/dpkg/mustlib.c
index 52d97527b..df1a91a61 100644
--- a/lib/dpkg/mustlib.c
+++ b/lib/dpkg/mustlib.c
@@ -141,8 +141,8 @@ setcloexec(int fd, const char *fn)
   int f;
 
   f = fcntl(fd, F_GETFD);
-  if (f == -1)
+  if (f < 0)
     ohshite(_("unable to read filedescriptor flags for %.250s"),fn);
-  if (fcntl(fd, F_SETFD, (f|FD_CLOEXEC))==-1)
+  if (fcntl(fd, F_SETFD, (f | FD_CLOEXEC)) < 0)
     ohshite(_("unable to set close-on-exec flag for %.250s"),fn);
 }
diff --git a/lib/dpkg/parse.c b/lib/dpkg/parse.c
index 120831a86..da9962429 100644
--- a/lib/dpkg/parse.c
+++ b/lib/dpkg/parse.c
@@ -555,7 +555,7 @@ parsedb_open(const char *filename, enum parsedbflags flags)
     return parsedb_new(filename, STDIN_FILENO, flags);
 
   fd = open(filename, O_RDONLY);
-  if (fd == -1 && !(errno == ENOENT && (flags & pdb_allow_empty)))
+  if (fd < 0 && !(errno == ENOENT && (flags & pdb_allow_empty)))
     ohshite(_("failed to open package info file '%.255s' for reading"),
             filename);
 
@@ -577,7 +577,7 @@ parsedb_load(struct parsedb_state *ps)
   if (ps->fd < 0 && (ps->flags & pdb_allow_empty))
       return;
 
-  if (fstat(ps->fd, &st) == -1)
+  if (fstat(ps->fd, &st) < 0)
     ohshite(_("can't stat package info file '%.255s'"), ps->filename);
 
   if (S_ISFIFO(st.st_mode)) {
diff --git a/lib/dpkg/subproc.c b/lib/dpkg/subproc.c
index 7b3fb9906..e91bb6215 100644
--- a/lib/dpkg/subproc.c
+++ b/lib/dpkg/subproc.c
@@ -104,7 +104,7 @@ subproc_fork(void)
        pid_t pid;
 
        pid = fork();
-       if (pid == -1) {
+       if (pid < 0) {
                onerr_abort++;
                ohshite(_("fork failed"));
        }
@@ -169,7 +169,7 @@ subproc_wait(pid_t pid, const char *desc)
        pid_t dead_pid;
        int status;
 
-       while ((dead_pid = waitpid(pid, &status, 0)) == -1 && errno == EINTR) ;
+       while ((dead_pid = waitpid(pid, &status, 0)) < 0 && errno == EINTR) ;
 
        if (dead_pid != pid) {
                onerr_abort++;
diff --git a/lib/dpkg/t/t-subproc.c b/lib/dpkg/t/t-subproc.c
index 7ce610b2c..8846d9076 100644
--- a/lib/dpkg/t/t-subproc.c
+++ b/lib/dpkg/t/t-subproc.c
@@ -62,13 +62,13 @@ test_subproc_fork(void)
        if (pid == 0)
                raise(SIGINT);
        ret = subproc_reap(pid, "subproc signal", SUBPROC_WARN);
-       test_pass(ret == -1);
+       test_pass(ret < 0);
 
        pid = subproc_fork();
        if (pid == 0)
                raise(SIGTERM);
        ret = subproc_reap(pid, "subproc signal", SUBPROC_WARN);
-       test_pass(ret == -1);
+       test_pass(ret < 0);
 
        pid = subproc_fork();
        if (pid == 0)
@@ -81,7 +81,7 @@ test_subproc_fork(void)
        if (pid == 0)
                raise(SIGPIPE);
        ret = subproc_reap(pid, "subproc SIGPIPE", SUBPROC_WARN);
-       test_pass(ret == -1);
+       test_pass(ret < 0);
 }
 
 TEST_ENTRY(test)
diff --git a/lib/dpkg/trigdeferred.c b/lib/dpkg/trigdeferred.c
index df34dea38..ae31d6285 100644
--- a/lib/dpkg/trigdeferred.c
+++ b/lib/dpkg/trigdeferred.c
@@ -77,9 +77,9 @@ trigdef_update_start(enum trigdef_update_flags uf)
 
        if (uf & TDUF_WRITE) {
                constructfn(&fn, triggersdir, TRIGGERSLOCKFILE);
-               if (lock_fd == -1) {
+               if (lock_fd < 0) {
                        lock_fd = open(fn.buf, O_RDWR | O_CREAT | O_TRUNC, 
0600);
-                       if (lock_fd == -1) {
+                       if (lock_fd < 0) {
                                if (!(errno == ENOENT && (uf & 
TDUF_NO_LOCK_OK)))
                                        ohshite(_("unable to open/create "
                                                  "triggers lock file 
'%.250s'"),
diff --git a/src/common/selinux.c b/src/common/selinux.c
index b5d29d829..2b0a8bcb6 100644
--- a/src/common/selinux.c
+++ b/src/common/selinux.c
@@ -130,7 +130,7 @@ dpkg_selabel_set_context(const char *matchpath, const char 
*path, mode_t mode)
        /* Do nothing if we can't figure out what the context is, or if it has
         * no context; in which case the default context shall be applied. */
        ret = selabel_lookup_raw(sehandle, &scontext, matchpath, mode & S_IFMT);
-       if (ret == -1 || (ret == 0 && scontext == NULL))
+       if (ret < 0 || (ret == 0 && scontext == NULL))
                return;
 
        ret = lsetfilecon_raw(path, scontext);
diff --git a/src/deb/build.c b/src/deb/build.c
index 5da2e76cd..a418dd122 100644
--- a/src/deb/build.c
+++ b/src/deb/build.c
@@ -616,7 +616,7 @@ do_build(const char *const *argv)
    * unlink our temporary file so others can't mess with it. */
   tfbuf = path_make_temp_template("dpkg-deb");
   gzfd = mkstemp(tfbuf);
-  if (gzfd == -1)
+  if (gzfd < 0)
     ohshite(_("failed to make temporary file (%s)"), _("control member"));
   /* Make sure it's gone, the fd will remain until we close it. */
   if (unlink(tfbuf))
@@ -688,7 +688,7 @@ do_build(const char *const *argv)
      * temporary file so others can't mess with it. */
     tfbuf = path_make_temp_template("dpkg-deb");
     gzfd = mkstemp(tfbuf);
-    if (gzfd == -1)
+    if (gzfd < 0)
       ohshite(_("failed to make temporary file (%s)"), _("data member"));
     /* Make sure it's gone, the fd will remain until we close it. */
     if (unlink(tfbuf))
diff --git a/src/deb/info.c b/src/deb/info.c
index fd00fd480..d2d72cf38 100644
--- a/src/deb/info.c
+++ b/src/deb/info.c
@@ -159,7 +159,7 @@ info_list(const char *debar, const char *dir)
   FILE *cc;
 
   cdn = scandir(dir, &cdlist, &ilist_select, alphasort);
-  if (cdn == -1)
+  if (cdn < 0)
     ohshite(_("cannot scan directory '%.255s'"), dir);
 
   for (n = 0; n < cdn; n++) {
diff --git a/utils/start-stop-daemon.c b/utils/start-stop-daemon.c
index 5e1ddeb38..345346265 100644
--- a/utils/start-stop-daemon.c
+++ b/utils/start-stop-daemon.c
@@ -546,7 +546,7 @@ wait_for_child(pid_t pid)
 
        do {
                child = waitpid(pid, &status, 0);
-       } while (child == -1 && errno == EINTR);
+       } while (child < 0 && errno == EINTR);
 
        if (child != pid)
                fatal("error waiting for child");
@@ -798,7 +798,7 @@ daemonize(void)
         * performing actions, such as creating a pidfile. */
        sigemptyset(&mask);
        sigaddset(&mask, SIGCHLD);
-       if (sigprocmask(SIG_BLOCK, &mask, &oldmask) == -1)
+       if (sigprocmask(SIG_BLOCK, &mask, &oldmask) < 0)
                fatale("cannot block SIGCHLD");
 
        if (notify_await)
@@ -849,7 +849,7 @@ daemonize(void)
                _exit(0);
        }
 
-       if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1)
+       if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
                fatale("cannot restore signal mask");
 
        debug("Detaching complete...\n");
@@ -1140,7 +1140,7 @@ set_proc_schedule(struct res_schedule *sched)
 
        param.sched_priority = sched->priority;
 
-       if (sched_setscheduler(getpid(), sched->policy, &param) == -1)
+       if (sched_setscheduler(getpid(), sched->policy, &param) < 0)
                fatale("unable to set process scheduler");
 #endif
 }
@@ -1160,7 +1160,7 @@ set_io_schedule(struct res_schedule *sched)
        int io_sched_mask;
 
        io_sched_mask = IOPRIO_PRIO_VALUE(sched->policy, sched->priority);
-       if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), io_sched_mask) == -1)
+       if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), io_sched_mask) < 0)
                warning("unable to alter IO priority to mask %i (%s)\n",
                        io_sched_mask, strerror(errno));
 #endif
@@ -1734,7 +1734,7 @@ pid_is_exec(pid_t pid, const struct stat *esb)
 
        sprintf(lname, "/proc/%d/exe", pid);
        nread = readlink(lname, lcontents, sizeof(lcontents) - 1);
-       if (nread == -1)
+       if (nread < 0)
                return false;
 
        filename = lcontents;
diff --git a/utils/update-alternatives.c b/utils/update-alternatives.c
index 7d21a4d47..9909cde68 100644
--- a/utils/update-alternatives.c
+++ b/utils/update-alternatives.c
@@ -380,7 +380,7 @@ areadlink(const char *linkname)
 
        /* Read it and terminate the string properly */
        size = readlink(linkname, buf, st.st_size);
-       if (size == -1) {
+       if (size < 0) {
                int saved_errno = errno;
 
                free(buf);
@@ -400,13 +400,13 @@ spawn(const char *prog, const char *args[])
        int status;
 
        pid = fork();
-       if (pid == -1)
+       if (pid < 0)
                error(_("fork failed"));
        if (pid == 0) {
                execvp(prog, (char *const *)args);
                syserr(_("unable to execute %s (%s)"), prog, prog);
        }
-       while ((dead_pid = waitpid(pid, &status, 0)) == -1 && errno == EINTR) ;
+       while ((dead_pid = waitpid(pid, &status, 0)) < 0 && errno == EINTR) ;
        if (dead_pid != pid)
                error(_("wait for subprocess %s failed"), prog);
 
@@ -1530,7 +1530,7 @@ alternative_load(struct alternative *a, enum altdb_flags 
flags)
        }
 
        /* Verify the alternative is not empty. */
-       if (fstat(fileno(ctx.fh), &st) == -1)
+       if (fstat(fileno(ctx.fh), &st) < 0)
                syserr(_("cannot stat file '%s'"), ctx.filename);
        if (st.st_size == 0) {
                altdb_context_free(&ctx);
@@ -1958,7 +1958,7 @@ alternative_path_classify(const char *linkname)
 {
        struct stat st;
 
-       if (fsys_lstat(linkname, &st) == -1) {
+       if (fsys_lstat(linkname, &st) < 0) {
                if (errno != ENOENT)
                        syserr(_("cannot stat file '%s%s'"), instdir, linkname);
                return ALT_PATH_MISSING;

-- 
Dpkg.Org's dpkg

Reply via email to