aboutsummaryrefslogtreecommitdiff
path: root/src/FbCommands.cc
diff options
context:
space:
mode:
authorMathias Gumz <akira at fluxbox dot org>2010-09-17 21:43:24 (GMT)
committerMathias Gumz <akira at fluxbox dot org>2010-09-17 21:43:24 (GMT)
commit87b45bd0d18323898d5c09bb7cad7566e3267635 (patch)
treef5570d524399e3dab443dde9419af86c3874f416 /src/FbCommands.cc
parentf3ad09c4ce70cc58871eeac49d640f213f3cd16f (diff)
downloadfluxbox-87b45bd0d18323898d5c09bb7cad7566e3267635.zip
fluxbox-87b45bd0d18323898d5c09bb7cad7566e3267635.tar.bz2
bugfix: avoid naive use of 'putenv' by providing 'FbTk::App::setenv()'
to quote from 'man putenv': The string pointed to by string becomes part of the environment, so altering the string changes the environment. so, using putenv like { std::string foo("FOO=bar"); putenv(foo.c_str()); } is wrong and leads to a potentially corrupted environment. valgrind complaint correctly. FbTk::App seems to be the appropriate place to hold '::seten()' because it alters the environment of the application.
Diffstat (limited to 'src/FbCommands.cc')
-rw-r--r--src/FbCommands.cc39
1 files changed, 5 insertions, 34 deletions
diff --git a/src/FbCommands.cc b/src/FbCommands.cc
index 8683711..7ff689e 100644
--- a/src/FbCommands.cc
+++ b/src/FbCommands.cc
@@ -142,8 +142,7 @@ int ExecuteCmd::run() {
142 if (pid) 142 if (pid)
143 return pid; 143 return pid;
144 144
145 string displaystring("DISPLAY="); 145 string display = DisplayString(FbTk::App::instance()->display());
146 displaystring += DisplayString(FbTk::App::instance()->display());
147 int screen_num = m_screen_num; 146 int screen_num = m_screen_num;
148 if (screen_num < 0) { 147 if (screen_num < 0) {
149 if (Fluxbox::instance()->mouseScreen() == 0) 148 if (Fluxbox::instance()->mouseScreen() == 0)
@@ -158,12 +157,11 @@ int ExecuteCmd::run() {
158 if (!shell) 157 if (!shell)
159 shell = "/bin/sh"; 158 shell = "/bin/sh";
160 159
161 // remove last number of display and add screen num 160 display.erase(display.size()-1);
162 displaystring.erase(displaystring.size()-1); 161 display += FbTk::StringUtil::number2String(screen_num);
163 displaystring += FbTk::StringUtil::number2String(screen_num);
164 162
165 setsid(); 163 setsid();
166 putenv(const_cast<char *>(displaystring.c_str())); 164 FbTk::App::setenv("DISPLAY", display.c_str());
167 execl(shell, shell, "-c", m_cmd.c_str(), static_cast<void*>(NULL)); 165 execl(shell, shell, "-c", m_cmd.c_str(), static_cast<void*>(NULL));
168 exit(EXIT_SUCCESS); 166 exit(EXIT_SUCCESS);
169 167
@@ -197,34 +195,7 @@ ExportCmd::ExportCmd(const string& name, const string& value) :
197 195
198void ExportCmd::execute() { 196void ExportCmd::execute() {
199 197
200 // the setenv()-routine is not everywhere available and 198 FbTk::App::instance()->setenv(m_name.c_str(), m_value.c_str());
201 // putenv() doesnt manage the strings in the environment
202 // and hence we have to do that on our own to avoid memleaking
203 static set<char*> stored;
204 char* newenv = new char[m_name.size() + m_value.size() + 2];
205 if (newenv) {
206
207 char* oldenv = getenv(m_name.c_str());
208
209 // oldenv points to the value .. we have to go back a bit
210 if (oldenv && stored.find(oldenv - (m_name.size() + 1)) != stored.end())
211 oldenv -= (m_name.size() + 1);
212 else
213 oldenv = NULL;
214
215 memset(newenv, 0, m_name.size() + m_value.size() + 2);
216 strcat(newenv, m_name.c_str());
217 strcat(newenv, "=");
218 strcat(newenv, m_value.c_str());
219
220 if (putenv(newenv) == 0) {
221 if (oldenv) {
222 stored.erase(oldenv);
223 delete[] oldenv;
224 }
225 stored.insert(newenv);
226 }
227 }
228} 199}
229 200
230REGISTER_COMMAND(exit, FbCommands::ExitFluxboxCmd, void); 201REGISTER_COMMAND(exit, FbCommands::ExitFluxboxCmd, void);