Refactor XkbFindFileInPath Also fixes a bug, where the check (typeLen < 1) should have been (pathLen < 1). Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
diff --git a/src/xkbcomp/xkbpath.c b/src/xkbcomp/xkbpath.c
index a08d265..4faa5b7 100644
--- a/src/xkbcomp/xkbpath.c
+++ b/src/xkbcomp/xkbpath.c
@@ -26,6 +26,7 @@
#define DEBUG_VAR debugFlags
#include "utils.h"
+#include <errno.h>
#include <stdlib.h>
#include "xkbpath.h"
#include "xkbcommon/xkbcommon.h"
@@ -315,9 +316,8 @@ XkbDirectoryForInclude(unsigned type)
FILE *
XkbFindFileInPath(const char *name, unsigned type, char **pathRtrn)
{
- int i;
+ int i, ret;
FILE *file = NULL;
- int nameLen, typeLen, pathLen;
char buf[PATH_MAX];
const char *typeDir;
@@ -325,32 +325,31 @@ XkbFindFileInPath(const char *name, unsigned type, char **pathRtrn)
return NULL;
typeDir = XkbDirectoryForInclude(type);
- nameLen = strlen(name);
- typeLen = strlen(typeDir);
for (i = 0; i < nPathEntries; i++)
{
- pathLen = strlen(includePath[i]);
- if (typeLen < 1)
+ if (includePath[i] == NULL || *includePath[i] == '\0')
continue;
- if ((nameLen + typeLen + pathLen + 2) >= PATH_MAX)
+ ret = snprintf(buf, sizeof(buf), "%s/%s/%s",
+ includePath[i], typeDir, name);
+ if (ret >= sizeof(buf))
{
ERROR("File name (%s/%s/%s) too long\n", includePath[i],
typeDir, name);
ACTION("Ignored\n");
continue;
}
- snprintf(buf, sizeof(buf), "%s/%s/%s", includePath[i], typeDir, name);
file = fopen(buf, "r");
- if (file != NULL)
- break;
+ if (file == NULL) {
+ ERROR("Couldn't open file (%s/%s/%s): %s\n", includePath[i],
+ typeDir, name, strerror(-errno));
+ ACTION("Ignored\n");
+ continue;
+ }
+ break;
}
if ((file != NULL) && (pathRtrn != NULL))
- {
- *pathRtrn = calloc(strlen(buf) + 1, sizeof(char));
- if (*pathRtrn != NULL)
- strcpy(*pathRtrn, buf);
- }
+ *pathRtrn = strdup(buf);
return file;
}