Simpler code to set _NET_DESKTOP_NAMES

gcc-4.2.1 on OpenBSD-5.6 hinted that strcpy() is not the safest function on
earth. While seeing the code I wondered why it we first create copies
of the names at all (let alone using memset() and then strcpy() after it).
This commit is contained in:
Mathias Gumz 2015-01-29 11:38:48 +01:00
parent 8e5a10ea6c
commit 8b44c7a184

View file

@ -821,17 +821,15 @@ void Ewmh::updateWorkspaceNames(BScreen &screen) {
const BScreen::WorkspaceNames &workspacenames = screen.getWorkspaceNames(); const BScreen::WorkspaceNames &workspacenames = screen.getWorkspaceNames();
const size_t number_of_desks = workspacenames.size(); const size_t number_of_desks = workspacenames.size();
char** names = new char*[number_of_desks]; const char** names = new const char*[number_of_desks];
for (size_t i = 0; i < number_of_desks; i++) { for (size_t i = 0; i < number_of_desks; i++) {
names[i] = new char[workspacenames[i].size() + 1]; // +1 for \0 names[i] = workspacenames[i].c_str();
memset(names[i], 0, workspacenames[i].size());
strcpy(names[i], workspacenames[i].c_str());
} }
#ifdef X_HAVE_UTF8_STRING #ifdef X_HAVE_UTF8_STRING
int code = Xutf8TextListToTextProperty(FbTk::App::instance()->display(), int code = Xutf8TextListToTextProperty(FbTk::App::instance()->display(),
names, number_of_desks, XUTF8StringStyle, &text); const_cast<char**>(names), number_of_desks, XUTF8StringStyle, &text);
if (code != XNoMemory && code != XLocaleNotSupported) { if (code != XNoMemory && code != XLocaleNotSupported) {
XSetTextProperty(FbTk::App::instance()->display(), XSetTextProperty(FbTk::App::instance()->display(),
screen.rootWindow().window(), screen.rootWindow().window(),
@ -848,11 +846,7 @@ void Ewmh::updateWorkspaceNames(BScreen &screen) {
} }
#endif #endif
for (size_t i = 0; i < number_of_desks; i++)
delete[] names[i];
delete[] names; delete[] names;
} }
void Ewmh::updateCurrentWorkspace(BScreen &screen) { void Ewmh::updateCurrentWorkspace(BScreen &screen) {