Skip to content

Commit 8339bbe

Browse files
committed
user_mailer_env: Quiet dead code warning in bounds check
1 parent 1a94eba commit 8339bbe

1 file changed

Lines changed: 31 additions & 28 deletions

File tree

lib/eventlog/eventlog.c

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ static char **
281281
user_mailer_env(const char *user)
282282
{
283283
char **envp;
284-
int envc = 4;
285-
int i = 0;
284+
int i, envc = 4;
285+
debug_decl(user_mailer_env, SUDO_DEBUG_UTIL);
286286

287287
#ifdef _AIX
288288
envc++; /* for LOGIN variable */
@@ -296,42 +296,45 @@ user_mailer_env(const char *user)
296296
if (envp == NULL)
297297
goto bad;
298298

299-
if (i >= envc)
300-
goto bad;
301-
if ((envp[i++] = strdup("HOME=/")) == NULL)
302-
goto bad;
303-
304-
if (i >= envc)
305-
goto bad;
306-
if ((envp[i++] = strdup("PATH=" _PATH_STDPATH)) == NULL)
307-
goto bad;
308-
309-
if (i >= envc)
310-
goto bad;
311-
if (asprintf(&envp[i++], "LOGNAME=%s", user) == -1)
312-
goto bad;
313-
314-
if (i >= envc)
315-
goto bad;
316-
if (asprintf(&envp[i++], "USER=%s", user) == -1)
317-
goto bad;
318-
299+
for (i = 0; i < envc; i++) {
300+
switch (i) {
301+
case 0:
302+
if ((envp[i] = strdup("HOME=/")) == NULL)
303+
goto bad;
304+
break;
305+
case 1:
306+
if ((envp[i] = strdup("PATH=" _PATH_STDPATH)) == NULL)
307+
goto bad;
308+
break;
309+
case 2:
310+
if (asprintf(&envp[i], "LOGNAME=%s", user) == -1)
311+
goto bad;
312+
break;
313+
case 3:
314+
if (asprintf(&envp[i], "USER=%s", user) == -1)
315+
goto bad;
316+
break;
319317
#ifdef _AIX
320-
if (i >= envc)
321-
goto bad;
322-
if (asprintf(&envp[i++], "LOGIN=%s", user) == -1)
323-
goto bad;
318+
case 4:
319+
if (asprintf(&envp[i], "LOGIN=%s", user) == -1)
320+
goto bad;
321+
break;
324322
#endif /* _AIX */
323+
default:
324+
sudo_warnx(U_("internal error, %s overflow"), __func__);
325+
goto bad;
326+
}
327+
}
325328

326-
return envp;
329+
debug_return_ptr(envp);
327330
bad:
328331
if (envp != NULL) {
329332
for (i = 0; i < envc && envp[i] != NULL; i++) {
330333
free(envp[i]);
331334
}
332335
free(envp);
333336
}
334-
return NULL;
337+
debug_return_ptr(NULL);
335338
}
336339

337340
#define MAX_MAILFLAGS 63

0 commit comments

Comments
 (0)