]> git.ipfire.org Git - thirdparty/git.git/commitdiff
trace2: refactor to avoid gcc warning under -O3
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Thu, 20 May 2021 11:05:45 +0000 (13:05 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 20 May 2021 21:56:11 +0000 (06:56 +0900)
Refactor tr2_dst_try_uds_connect() to avoid a gcc warning[1] that
appears under -O3 (but not -O2). This makes the build pass under
DEVELOPER=1 without needing a DEVOPTS=no-error.

This can be reproduced with GCC Debian 8.3.0-6, but not e.g. with
clang 7.0.1-8+deb10u2. We've had this warning since
ee4512ed481 (trace2: create new combined trace facility, 2019-02-22).

As noted in [2] this warning happens because the compiler doesn't
assume that errno must be non-zero after a failed syscall.

Let's work around by using the well-established "saved_errno" pattern,
along with returning -1 ourselves instead of "errno". The caller can
thus rely on our "errno" on failure.

See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61846 for a related
bug report against GCC.

1.

    trace2/tr2_dst.c: In function ‘tr2_dst_get_trace_fd.part.5’:
    trace2/tr2_dst.c:296:10: warning: ‘fd’ may be used uninitialized in this function [-Wmaybe-uninitialized]
      dst->fd = fd;
      ~~~~~~~~^~~~
    trace2/tr2_dst.c:229:6: note: ‘fd’ was declared here
      int fd;
          ^~
2. https://lore.kernel.org/git/20200404142131.GA679473@coredump.intra.peff.net/

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
trace2/tr2_dst.c

index 5dda0ca1cdb5d09a8cdfb27dd45d4436f3e8aa6f..00314763505b0344ba881e76854a1ef3d7808150 100644 (file)
@@ -115,15 +115,16 @@ static int tr2_dst_try_uds_connect(const char *path, int sock_type, int *out_fd)
 
        fd = socket(AF_UNIX, sock_type, 0);
        if (fd == -1)
-               return errno;
+               return -1;
 
        sa.sun_family = AF_UNIX;
        strlcpy(sa.sun_path, path, sizeof(sa.sun_path));
 
        if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
-               int e = errno;
+               int saved_errno = errno;
                close(fd);
-               return e;
+               errno = saved_errno;
+               return -1;
        }
 
        *out_fd = fd;
@@ -138,7 +139,6 @@ static int tr2_dst_try_unix_domain_socket(struct tr2_dst *dst,
 {
        unsigned int uds_try = 0;
        int fd;
-       int e;
        const char *path = NULL;
 
        /*
@@ -182,15 +182,13 @@ static int tr2_dst_try_unix_domain_socket(struct tr2_dst *dst,
        }
 
        if (uds_try & TR2_DST_UDS_TRY_STREAM) {
-               e = tr2_dst_try_uds_connect(path, SOCK_STREAM, &fd);
-               if (!e)
+               if (!tr2_dst_try_uds_connect(path, SOCK_STREAM, &fd))
                        goto connected;
-               if (e != EPROTOTYPE)
+               if (errno != EPROTOTYPE)
                        goto error;
        }
        if (uds_try & TR2_DST_UDS_TRY_DGRAM) {
-               e = tr2_dst_try_uds_connect(path, SOCK_DGRAM, &fd);
-               if (!e)
+               if (!tr2_dst_try_uds_connect(path, SOCK_DGRAM, &fd))
                        goto connected;
        }
 
@@ -198,7 +196,7 @@ error:
        if (tr2_dst_want_warning())
                warning("trace2: could not connect to socket '%s' for '%s' tracing: %s",
                        path, tr2_sysenv_display_name(dst->sysenv_var),
-                       strerror(e));
+                       strerror(errno));
 
        tr2_dst_trace_disable(dst);
        return 0;