Skip to content

Commit b9a4533

Browse files
committed
Replaces unchecked strcpy/strcat with strlcpy/strlcat
Unchecked writes to strings using strcpy and strcat can cause memory smashes, replacing them with (destination) bounds checked strl... equivalents can avoid this. Incidentally, fix construction of file name for $HOME/.Xdefaults
1 parent acf41ce commit b9a4533

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/xrdopt.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <limits.h> // for PATH_MAX
1818
#include <stdio.h> // for fprintf, NULL, stderr, sscanf
1919
#include <stdlib.h> // for getenv, exit, strtol
20-
#include <string.h> // for strncpy, strcat, strcpy, strcmp
20+
#include <string.h> // for strncpy, strlcat, strlcpy, strcmp
2121
#include <sys/types.h> // for u_char
2222
#include <unistd.h> // for access, R_OK
2323
#include "xdefs.h" // for WINDOW_NAME
@@ -211,13 +211,13 @@ void read_Xoption(int *argc, char *argv[])
211211
print_Xusage(argv[0]);
212212
} else {
213213
envname = getenv("DISPLAY");
214-
(void)strcpy(Display_Name, envname);
214+
(void)strlcpy(Display_Name, envname, sizeof(Display_Name));
215215
}
216216
if ((xdisplay = XOpenDisplay(Display_Name)) != NULL) {
217217
/* read the other databases */
218218
/* Start with app-defaults/medley */
219-
(void)strcpy(tmp, "/usr/lib/X11/app-defaults/");
220-
(void)strcat(tmp, "medley");
219+
(void)strlcpy(tmp, "/usr/lib/X11/app-defaults/", sizeof(tmp));
220+
(void)strlcat(tmp, "medley", sizeof(tmp));
221221
applicationDB = XrmGetFileDatabase(tmp);
222222
if (applicationDB != NULL) { (void)XrmMergeDatabases(applicationDB, &rDB); }
223223
/* Then try the displays defaults */
@@ -232,8 +232,8 @@ void read_Xoption(int *argc, char *argv[])
232232
}
233233

234234
envname = getenv("HOME");
235-
(void)strcat(tmp, envname);
236-
(void)strcat(tmp, "/.Xdefaults");
235+
(void)strlcpy(tmp, envname, sizeof(tmp));
236+
(void)strlcat(tmp, "/.Xdefaults", sizeof(tmp));
237237
if (access(tmp, R_OK) != 0) {
238238
serverDB = XrmGetFileDatabase(tmp);
239239
if (serverDB != NULL) { (void)XrmMergeDatabases(serverDB, &rDB); }
@@ -255,7 +255,7 @@ void read_Xoption(int *argc, char *argv[])
255255
if (XrmGetResource(rDB, "ldex.icontitle", "Ldex.icontitle", str_type, &value) == True) {
256256
(void)strncpy(iconTitle, value.addr, value.size);
257257
} else {
258-
(void)strcpy(iconTitle, "Medley");
258+
(void)strlcpy(iconTitle, "Medley", sizeof(iconTitle));
259259
}
260260

261261
if (XrmGetResource(rDB, "ldex.iconbitmap", "Ldex.Iconbitmap", str_type, &value) == True) {
@@ -276,8 +276,6 @@ void read_Xoption(int *argc, char *argv[])
276276
&LispDisplayRequestedWidth, &LispDisplayRequestedHeight);
277277
}
278278

279-
(void)strcpy(tmp, ""); /* Clear the string */
280-
281279
if (XrmGetResource(rDB, "ldex.cursorColor", "Ldex.cursorColor", str_type, &value) == True) {
282280
(void)strncpy(cursorColor, value.addr, sizeof(cursorColor) - 1);
283281
}

0 commit comments

Comments
 (0)