Coverity CIDs 468127 468134 468148

These Coverity warnings all relate to the user of g_setenv() where the
return result isn't checked.

An additional void function g_setenv_log() is provided which logs
failures to set environment variables, and returns no status. This is
used in all the places where g_setenv_is currently called.
This commit is contained in:
matt335672
2025-04-17 14:36:25 +01:00
parent f2dd3fb3dc
commit 417076b215
7 changed files with 55 additions and 31 deletions
+17
View File
@@ -3589,6 +3589,23 @@ g_setenv(const char *name, const char *value, int rewrite)
#endif
}
/*****************************************************************************/
/* does not work in win32 */
void
g_setenv_log(const char *name, const char *value, int rewrite)
{
#if defined(_WIN32)
return 0;
#else
if (setenv(name, value, rewrite) != 0)
{
LOG(LOG_LEVEL_WARNING, "Unable to set environment variable '%s' [%s]",
name, g_get_strerror());
}
#endif
}
/*****************************************************************************/
/* does not work in win32 */
char *
+11
View File
@@ -381,6 +381,17 @@ int g_setpgid(int pid, int pgid);
void g_clearenv(void);
int g_setenv(const char *name, const char *value, int rewrite);
char *g_getenv(const char *name);
/**
* Calls g_setenv(), logging failures
*
* @param name Name to set
* @param value String to set $name to
* @param rewrite Set to non-zero to allow rewriting of existing names
*
* Unlike g_setenv() this function returns no value. Use this function if the
* only reasonable thing to do on failure is to log it.
*/
void g_setenv_log(const char *name, const char *value, int rewrite);
int g_exit(int exit_code);
int g_getpid(void);
int g_sigterm(int pid);
+4 -4
View File
@@ -528,12 +528,12 @@ auth_set_env(struct auth_info *auth_info)
for (pam_env = pam_envlist; *pam_env != NULL; ++pam_env)
{
char *str = *pam_env;
int eq_pos = g_pos(str, "=");
char *eq_pos = strchr(str, '=');
if (eq_pos > 0)
if (eq_pos != NULL)
{
str[eq_pos] = '\0';
g_setenv(str, str + eq_pos + 1, 1);
*eq_pos = '\0';
g_setenv_log(str, eq_pos + 1, 1);
}
g_free(str);
+4 -4
View File
@@ -338,12 +338,12 @@ auth_set_env(struct auth_info *auth_info)
for (pam_env = pam_envlist; *pam_env != NULL; ++pam_env)
{
char *str = *pam_env;
int eq_pos = g_pos(str, "=");
char *eq_pos = strchr(str, '=');
if (eq_pos > 0)
if (eq_pos != NULL)
{
str[eq_pos] = '\0';
g_setenv(str, str + eq_pos + 1, 1);
*eq_pos = '\0';
g_setenv_log(str, eq_pos + 1, 1);
}
g_free(str);
+13 -13
View File
@@ -144,38 +144,38 @@ env_set_user(int uid, char **passwd_file, int display,
if (error == 0)
{
g_setenv("PATH", "/sbin:/bin:/usr/bin:/usr/local/bin", 1);
g_setenv_log("PATH", "/sbin:/bin:/usr/bin:/usr/local/bin", 1);
}
#endif
if (error == 0)
{
g_setenv("SHELL", pw_shell, 1);
g_setenv("USER", pw_username, 1);
g_setenv("LOGNAME", pw_username, 1);
g_setenv_log("SHELL", pw_shell, 1);
g_setenv_log("USER", pw_username, 1);
g_setenv_log("LOGNAME", pw_username, 1);
g_snprintf(text, sizeof(text), "%d", uid);
g_setenv("UID", text, 1);
g_setenv("HOME", pw_dir, 1);
g_setenv_log("UID", text, 1);
g_setenv_log("HOME", pw_dir, 1);
g_set_current_dir(pw_dir);
g_snprintf(text, sizeof(text), ":%d.0", display);
g_setenv("DISPLAY", text, 1);
g_setenv_log("DISPLAY", text, 1);
// Use our PID as the XRDP_SESSION value
g_snprintf(text, sizeof(text), "%d", g_pid);
g_setenv("XRDP_SESSION", text, 1);
g_setenv_log("XRDP_SESSION", text, 1);
/* XRDP_SOCKET_PATH should be set here. It's used by
* xorgxrdp and the pulseaudio plugin */
g_snprintf(text, sizeof(text), XRDP_SOCKET_PATH, uid);
g_setenv("XRDP_SOCKET_PATH", text, 1);
g_setenv_log("XRDP_SOCKET_PATH", text, 1);
/* pulse sink socket */
g_snprintf(text, sizeof(text), CHANSRV_PORT_OUT_BASE_STR, display);
g_setenv("XRDP_PULSE_SINK_SOCKET", text, 1);
g_setenv_log("XRDP_PULSE_SINK_SOCKET", text, 1);
/* pulse source socket */
g_snprintf(text, sizeof(text), CHANSRV_PORT_IN_BASE_STR, display);
g_setenv("XRDP_PULSE_SOURCE_SOCKET", text, 1);
g_setenv_log("XRDP_PULSE_SOURCE_SOCKET", text, 1);
if (g_cfg->sec.xauth_in_sysdir)
{
g_snprintf(text, sizeof(text), XRDP_SOCKET_PATH "/Xauthority",
uid);
g_setenv("XAUTHORITY", text, 1);
g_setenv_log("XAUTHORITY", text, 1);
}
if ((env_names != 0) && (env_values != 0) &&
(env_names->count == env_values->count))
@@ -184,7 +184,7 @@ env_set_user(int uid, char **passwd_file, int display,
{
name = (char *) list_get_item(env_names, index),
value = (char *) list_get_item(env_values, index),
g_setenv(name, value, 1);
g_setenv_log(name, value, 1);
}
}
g_gethostname(hostname, 255);
+5 -5
View File
@@ -357,19 +357,19 @@ prepare_xorg_xserver_params(const struct session_parameters *s,
/* some args are passed via env vars */
g_snprintf(text, sizeof(text), "%d", s->width);
g_setenv("XRDP_START_WIDTH", text, 1);
g_setenv_log("XRDP_START_WIDTH", text, 1);
g_snprintf(text, sizeof(text), "%d", s->height);
g_setenv("XRDP_START_HEIGHT", text, 1);
g_setenv_log("XRDP_START_HEIGHT", text, 1);
g_snprintf(text, sizeof(text), "%d", g_cfg->sess.max_idle_time);
g_setenv("XRDP_SESMAN_MAX_IDLE_TIME", text, 1);
g_setenv_log("XRDP_SESMAN_MAX_IDLE_TIME", text, 1);
g_snprintf(text, sizeof(text), "%d", g_cfg->sess.max_disc_time);
g_setenv("XRDP_SESMAN_MAX_DISC_TIME", text, 1);
g_setenv_log("XRDP_SESMAN_MAX_DISC_TIME", text, 1);
g_snprintf(text, sizeof(text), "%d", g_cfg->sess.kill_disconnected);
g_setenv("XRDP_SESMAN_KILL_DISCONNECTED", text, 1);
g_setenv_log("XRDP_SESMAN_KILL_DISCONNECTED", text, 1);
/* get path of Xorg from config */
xserver = (const char *)list_get_item(g_cfg->xorg_params, 0);
+1 -5
View File
@@ -165,11 +165,7 @@ sesexec_start(struct pre_session_item *psi)
* in the environment */
char buff[64];
g_snprintf(buff, sizeof(buff), "%d", sck[1]);
if (g_setenv("EICP_FD", buff, 1) < 0)
{
LOG(LOG_LEVEL_ERROR, "Can't set EICP_FD [%s]",
g_get_strerror());
}
g_setenv_log("EICP_FD", buff, 1);
/* [Development] Log all file descriptors not marked cloexec
* other than stdin, stdout, stderr, and the EICP fd in sck[1].