e: handle e_util_head_exec display env

Clean up the code to set DISPLAY env so it is safe and correct.
This commit is contained in:
Sebastian Dransfeld 2013-12-08 22:30:04 +01:00
parent 65a1ed752e
commit 5ca535383f
1 changed files with 9 additions and 19 deletions

View File

@ -148,34 +148,27 @@ e_util_head_exec(int head, const char *cmd)
{ {
char *penv_display; char *penv_display;
char *p1, *p2; char *p1, *p2;
char buf[4096], buf2[32]; char buf[4096];
int ok = 0; int ok = 0;
Ecore_Exe *exe; Ecore_Exe *exe;
penv_display = getenv("DISPLAY"); penv_display = getenv("DISPLAY");
if (!penv_display) return 0; if (!penv_display) return 0;
penv_display = strdup(penv_display); penv_display = strdup(penv_display);
if (!penv_display) return 0;
/* set env vars */ /* set env vars */
p1 = strrchr(penv_display, ':'); p1 = strrchr(penv_display, ':');
p2 = strrchr(penv_display, '.'); p2 = strrchr(penv_display, '.');
if ((p1) && (p2) && (p2 > p1)) /* "blah:x.y" */ if ((p1) && (p2) && (p2 > p1)) /* "blah:x.y" */
{ {
/* yes it could overflow... but who will overflow DISPLAY eh? why? to *p2 = 0;
* "exploit" your own applications running as you? snprintf(buf, sizeof(buf), "%s.%i", penv_display, head);
*/ *p2 = '.';
strncpy(buf, penv_display, sizeof(buf));
buf[p2 - penv_display + 1] = 0;
snprintf(buf2, sizeof(buf2), "%i", head);
strcat(buf, buf2);
} }
else if (p1) /* "blah:x */ else if (p1) /* "blah:x */
{ snprintf(buf, sizeof(buf), "%s.%i", penv_display, head);
strncpy(buf, penv_display, sizeof(buf));
snprintf(buf2, sizeof(buf2), ".%i", head);
strcat(buf, buf2);
}
else else
strncpy(buf, penv_display, sizeof(buf)); eina_strlcpy(buf, penv_display, sizeof(buf));
ok = 1; ok = 1;
exe = ecore_exe_run(cmd, NULL); exe = ecore_exe_run(cmd, NULL);
@ -190,11 +183,8 @@ e_util_head_exec(int head, const char *cmd)
} }
/* reset env vars */ /* reset env vars */
if (penv_display) e_util_env_set("DISPLAY", penv_display);
{ free(penv_display);
e_util_env_set("DISPLAY", penv_display);
free(penv_display);
}
return ok; return ok;
} }