Skip to content

Commit 4e0a64a

Browse files
avargitster
authored andcommitted
trace2: refactor to avoid gcc warning under -O3
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 ee4512e (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/[email protected]/ Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c753e2a commit 4e0a64a

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

trace2/tr2_dst.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,16 @@ static int tr2_dst_try_uds_connect(const char *path, int sock_type, int *out_fd)
115115

116116
fd = socket(AF_UNIX, sock_type, 0);
117117
if (fd == -1)
118-
return errno;
118+
return -1;
119119

120120
sa.sun_family = AF_UNIX;
121121
strlcpy(sa.sun_path, path, sizeof(sa.sun_path));
122122

123123
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
124-
int e = errno;
124+
int saved_errno = errno;
125125
close(fd);
126-
return e;
126+
errno = saved_errno;
127+
return -1;
127128
}
128129

129130
*out_fd = fd;
@@ -138,7 +139,6 @@ static int tr2_dst_try_unix_domain_socket(struct tr2_dst *dst,
138139
{
139140
unsigned int uds_try = 0;
140141
int fd;
141-
int e;
142142
const char *path = NULL;
143143

144144
/*
@@ -182,23 +182,21 @@ static int tr2_dst_try_unix_domain_socket(struct tr2_dst *dst,
182182
}
183183

184184
if (uds_try & TR2_DST_UDS_TRY_STREAM) {
185-
e = tr2_dst_try_uds_connect(path, SOCK_STREAM, &fd);
186-
if (!e)
185+
if (!tr2_dst_try_uds_connect(path, SOCK_STREAM, &fd))
187186
goto connected;
188-
if (e != EPROTOTYPE)
187+
if (errno != EPROTOTYPE)
189188
goto error;
190189
}
191190
if (uds_try & TR2_DST_UDS_TRY_DGRAM) {
192-
e = tr2_dst_try_uds_connect(path, SOCK_DGRAM, &fd);
193-
if (!e)
191+
if (!tr2_dst_try_uds_connect(path, SOCK_DGRAM, &fd))
194192
goto connected;
195193
}
196194

197195
error:
198196
if (tr2_dst_want_warning())
199197
warning("trace2: could not connect to socket '%s' for '%s' tracing: %s",
200198
path, tr2_sysenv_display_name(dst->sysenv_var),
201-
strerror(e));
199+
strerror(errno));
202200

203201
tr2_dst_trace_disable(dst);
204202
return 0;

0 commit comments

Comments
 (0)