From f6b8f8c071a575e54645aeb0bd3cb37377b0e4d3 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 18 Mar 2021 09:44:53 +1000 Subject: [PATCH] xkb: don't require a trailing slash for the XKM output dir Rework the function to use a single snprintf call instead of a mix of strcpy/strcats. This now also appends a trailing slash where needed so we don't rely on the build system to set this for us. Also, since /tmp/ is the fallback and we never check if everything succeeded, assert if we can't use /tmp/. This will never be triggered anyway, the only caller to OutputDirectory() uses sizeof(PATH_MAX-sized array). Follow-up from 6c51818a0f55282cbe5a870f58ca82ca45ee472d Signed-off-by: Peter Hutterer --- xkb/ddxLoad.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/xkb/ddxLoad.c b/xkb/ddxLoad.c index ea7e34700..f9b7b06d9 100644 --- a/xkb/ddxLoad.c +++ b/xkb/ddxLoad.c @@ -62,22 +62,27 @@ LoadXKM(unsigned want, unsigned need, const char *keymap, XkbDescPtr *xkbRtrn); static void OutputDirectory(char *outdir, size_t size) { + const char *directory = NULL; + const char *pathsep = ""; + int r = -1; + #ifndef WIN32 /* Can we write an xkm and then open it too? */ - if (access(XKM_OUTPUT_DIR, W_OK | X_OK) == 0 && - (strlen(XKM_OUTPUT_DIR) < size)) { - (void) strcpy(outdir, XKM_OUTPUT_DIR); + if (access(XKM_OUTPUT_DIR, W_OK | X_OK) == 0) { + directory = XKM_OUTPUT_DIR; + if (XKM_OUTPUT_DIR[strlen(XKM_OUTPUT_DIR) - 1] != '/') + pathsep = "/"; } - else #else - if (strlen(Win32TempDir()) + 1 < size) { - (void) strcpy(outdir, Win32TempDir()); - (void) strcat(outdir, "\\"); - } - else + directory = Win32TempDir(); + pathsep = "\\"; #endif - if (strlen("/tmp/") < size) { - (void) strcpy(outdir, "/tmp/"); + + if (directory) + r = snprintf(outdir, size, "%s%s", directory, pathsep); + if (r < 0 || r >= size) { + assert(strlen("/tmp/") < size); + strcpy(outdir, "/tmp/"); } }