diff options
author | Mathias Gumz <akira@fluxbox.org> | 2015-01-29 10:43:05 (GMT) |
---|---|---|
committer | Mathias Gumz <akira@fluxbox.org> | 2015-01-29 10:43:05 (GMT) |
commit | 129d0ac31efcefc21d083c1dc204d055c9269d82 (patch) | |
tree | 9ed9a939952e820b68b11fb3438f90a846861be8 /src | |
parent | 8b44c7a184e33aaacc6834b19efa88be058a4a4a (diff) | |
download | fluxbox-129d0ac31efcefc21d083c1dc204d055c9269d82.zip fluxbox-129d0ac31efcefc21d083c1dc204d055c9269d82.tar.bz2 |
Make fluxbox strcat() free
Again, gcc-4.2.1 of OpenBSD-5.6 pointed out the use of strcat(). I took the
chance to explain the reasons for the code a little bit.
Diffstat (limited to 'src')
-rw-r--r-- | src/FbTk/App.cc | 80 |
1 files changed, 41 insertions, 39 deletions
diff --git a/src/FbTk/App.cc b/src/FbTk/App.cc index f494a0e..a3e806a 100644 --- a/src/FbTk/App.cc +++ b/src/FbTk/App.cc | |||
@@ -23,23 +23,13 @@ | |||
23 | #include "FbString.hh" | 23 | #include "FbString.hh" |
24 | #include "Font.hh" | 24 | #include "Font.hh" |
25 | #include "Image.hh" | 25 | #include "Image.hh" |
26 | |||
27 | #include "EventManager.hh" | 26 | #include "EventManager.hh" |
28 | 27 | ||
29 | #ifdef HAVE_CSTRING | 28 | #include <cstring> |
30 | #include <cstring> | 29 | #include <cstdlib> |
31 | #else | ||
32 | #include <string.h> | ||
33 | #endif | ||
34 | #ifdef HAVE_CSTDLIB | ||
35 | #include <cstdlib> | ||
36 | #else | ||
37 | #include <stdlib.h> | ||
38 | #endif | ||
39 | |||
40 | 30 | ||
41 | #include <set> | 31 | #include <set> |
42 | 32 | #include <iostream> | |
43 | 33 | ||
44 | namespace FbTk { | 34 | namespace FbTk { |
45 | 35 | ||
@@ -108,35 +98,47 @@ bool App::setenv(const char* key, const char* value) { | |||
108 | 98 | ||
109 | const size_t key_size = strlen(key); | 99 | const size_t key_size = strlen(key); |
110 | const size_t value_size = value ? strlen(value) : 0; | 100 | const size_t value_size = value ? strlen(value) : 0; |
101 | const size_t newenv_size = key_size + value_size + 2; | ||
111 | 102 | ||
112 | char* newenv = new char[key_size + value_size + 2]; | 103 | char* newenv = new char[newenv_size]; |
113 | if (newenv) { | 104 | if (!newenv) { |
114 | 105 | return false; | |
115 | char* oldenv = getenv(key); | 106 | } |
116 | 107 | ||
117 | // oldenv points to the value .. we have to go back a bit | 108 | |
118 | if (oldenv && stored.find(oldenv - (key_size + 1)) != stored.end()) | 109 | // someone might have used putenv("key=value") (or setenv()) before. |
119 | oldenv -= (key_size + 1); | 110 | // if getenv("key") succeeds, the returning value points to the address |
120 | else | 111 | // of "value" (right after the "="). this means, that that address |
121 | oldenv = NULL; | 112 | // minus "key=" points to the beginning of what was set. if fluxbox |
122 | 113 | // set that variable we have the pointer in "stored" and can dealloc it. | |
123 | memset(newenv, 0, key_size + value_size + 2); | 114 | // |
124 | strcat(newenv, key); | 115 | // we use putenv() to have control over the dealloction (valgrind and |
125 | strcat(newenv, "="); | 116 | // other complaint about it) |
126 | if (value_size > 0) | 117 | |
127 | strcat(newenv, value); | 118 | char* oldenv = getenv(key); |
128 | 119 | if (oldenv) { | |
129 | if (putenv(newenv) == 0) { | 120 | oldenv = oldenv - (key_size + 1); |
130 | if (oldenv) { | 121 | } |
131 | stored.erase(oldenv); | 122 | if (stored.find(oldenv) == stored.end()) { |
132 | delete[] oldenv; | 123 | oldenv = NULL; |
133 | } | ||
134 | stored.insert(newenv); | ||
135 | } | ||
136 | return true; | ||
137 | } | 124 | } |
138 | 125 | ||
139 | return false; | 126 | // create the new environment |
127 | strncpy(newenv, key, key_size); | ||
128 | newenv[key_size] = '='; | ||
129 | if (value_size > 0) { | ||
130 | strncpy(newenv+key_size+1, value, value_size); | ||
131 | } | ||
132 | newenv[newenv_size-1] = 0; | ||
133 | |||
134 | if (putenv(newenv) == 0) { | ||
135 | if (oldenv) { | ||
136 | stored.erase(oldenv); | ||
137 | delete[] oldenv; | ||
138 | } | ||
139 | stored.insert(newenv); | ||
140 | } | ||
141 | return true; | ||
140 | } | 142 | } |
141 | 143 | ||
142 | } // end namespace FbTk | 144 | } // end namespace FbTk |