@@ -145,24 +145,22 @@ namespace platf {
145145 std::call_once (migration_flag, []() {
146146 bool found = false ;
147147 bool migrate_config = true ;
148- const char *dir;
149- const char *homedir;
150- const char *migrate_envvar;
148+ std::string homedir; // NOSONAR(cpp:S6010) - For get_env this needs to be a std::string reference
151149
152150 // Get the home directory
153- if ((homedir = getenv ( " HOME" )) == nullptr || strlen ( homedir) == 0 ) {
151+ if (! get_env ( " HOME" , homedir) || homedir. empty () ) {
154152 // If HOME is empty or not set, use the current user's home directory
155153 homedir = getpwuid (geteuid ())->pw_dir ;
156154 }
157155
158156 // May be set if running under a systemd service with the ConfigurationDirectory= option set.
159- if (( dir = getenv (" CONFIGURATION_DIRECTORY" )) != nullptr && strlen ( dir) > 0 ) {
157+ if (std::string dir; get_env (" CONFIGURATION_DIRECTORY" , dir) && ! dir. empty () ) {
160158 found = true ;
161159 config_path = fs::path (dir) / " sunshine" sv;
162160 }
163161 // Otherwise, follow the XDG base directory specification:
164162 // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
165- if (!found && (dir = getenv ( " XDG_CONFIG_HOME" )) != nullptr && strlen ( dir) > 0 ) {
163+ if (std::string dir; !found && get_env ( " XDG_CONFIG_HOME" , dir) && ! dir. empty () ) {
166164 found = true ;
167165 config_path = fs::path (dir) / " sunshine" sv;
168166 }
@@ -173,8 +171,7 @@ namespace platf {
173171 }
174172
175173 // migrate from the old config location if necessary
176- migrate_envvar = getenv (" SUNSHINE_MIGRATE_CONFIG" );
177- if (migrate_config && found && migrate_envvar && strcmp (migrate_envvar, " 1" ) == 0 ) {
174+ if (std::string migrate_envvar; migrate_config && found && get_env (" SUNSHINE_MIGRATE_CONFIG" , migrate_envvar) && (migrate_envvar == " 1" )) {
178175 std::error_code ec;
179176 fs::path old_config_path = fs::path (homedir) / " .config/sunshine" sv;
180177 if (old_config_path != config_path && fs::exists (old_config_path, ec)) {
@@ -322,7 +319,9 @@ namespace platf {
322319 */
323320 void open_url (const std::string &url) {
324321 // set working dir to user home directory
325- auto working_dir = boost::filesystem::path (std::getenv (" HOME" ));
322+ std::string homedir;
323+ get_env (" HOME" , homedir);
324+ auto working_dir = boost::filesystem::path (homedir);
326325 std::string cmd = R"( xdg-open ")" + url + R"( ")" ;
327326
328327 boost::process::v1::environment _env = boost::this_process::environment ();
@@ -445,24 +444,19 @@ namespace platf {
445444 lifetime::exit_sunshine (0 , true );
446445 }
447446
448- std::string get_env (const std::string &name) {
449- if (const auto value = getenv (name.c_str ()); value != nullptr ) {
450- return value;
447+ bool get_env (const std::string &name, std::string &value) {
448+ const auto value_ = getenv (name.c_str ()); // NOSONAR(cpp:S1874) - _dupenv_s is only available on WIN32
449+ if (value_ == nullptr ) {
450+ return false ;
451451 }
452- return " " ;
452+ value = std::string (value_);
453+ return true ;
453454 }
454455
455456 int set_env (const std::string &name, const std::string &value) {
456457 return setenv (name.c_str (), value.c_str (), 1 );
457458 }
458459
459- int append_env (const std::string &name, const std::string &value, const std::string &separator) {
460- if (const std::string old_value = get_env (name); !old_value.contains (value)) {
461- return set_env (name, old_value.empty () ? value : old_value + separator + value);
462- }
463- return 0 ;
464- }
465-
466460 int unset_env (const std::string &name) {
467461 return unsetenv (name.c_str ());
468462 }
@@ -1152,13 +1146,13 @@ namespace platf {
11521146
11531147 window_system = window_system_e::NONE ;
11541148#ifdef SUNSHINE_BUILD_WAYLAND
1155- if (std::getenv (" WAYLAND_DISPLAY" )) {
1149+ if (std::string v; get_env (" WAYLAND_DISPLAY" , v )) {
11561150 window_system = window_system_e::WAYLAND ;
11571151 }
11581152#endif
11591153#if defined(SUNSHINE_BUILD_X11) || defined(SUNSHINE_BUILD_CUDA)
1160- if (std::getenv (" DISPLAY" ) && window_system != window_system_e::WAYLAND ) {
1161- if (std::getenv (" WAYLAND_DISPLAY" )) {
1154+ if (std::string v; get_env (" DISPLAY" , v ) && window_system != window_system_e::WAYLAND ) {
1155+ if (get_env (" WAYLAND_DISPLAY" , v )) {
11621156 BOOST_LOG (warning) << " Wayland detected, yet sunshine will use X11 for screencasting, screencasting will only work on XWayland applications" sv;
11631157 }
11641158
0 commit comments