{
unsigned int i;
FILE *file = NULL;
- char buf[PATH_MAX];
+ char *buf = NULL;
const char *typeDir;
+ size_t buf_size = 0, typeDirLen, name_len;
typeDir = DirectoryForInclude(type);
+ typeDirLen = strlen(typeDir);
+ name_len = strlen(name);
for (i = 0; i < xkb_context_num_include_paths(ctx); i++) {
- int ret = snprintf(buf, sizeof(buf), "%s/%s/%s",
- xkb_context_include_path_get(ctx, i),
- typeDir, name);
- if (ret >= (ssize_t) sizeof(buf)) {
- log_err(ctx, "File name (%s/%s/%s) too long\n",
+ size_t new_buf_size = strlen(xkb_context_include_path_get(ctx, i)) +
+ typeDirLen + name_len + 3;
+ int ret;
+ if (new_buf_size > buf_size) {
+ void *buf_new = realloc(buf, new_buf_size);
+ if (buf_new) {
+ buf_size = new_buf_size;
+ buf = buf_new;
+ } else {
+ log_err(ctx, "Cannot realloc for name (%s/%s/%s)\n",
+ xkb_context_include_path_get(ctx, i), typeDir, name);
+ continue;
+ }
+ }
+ ret = snprintf(buf, buf_size, "%s/%s/%s",
+ xkb_context_include_path_get(ctx, i),
+ typeDir, name);
+ if (ret < 0) {
+ log_err(ctx, "snprintf error (%s/%s/%s)\n",
xkb_context_include_path_get(ctx, i), typeDir, name);
continue;
}
xkb_context_failed_include_path_get(ctx, i));
}
+ free(buf);
return NULL;
}
if (pathRtrn)
- *pathRtrn = strdup(buf);
+ *pathRtrn = buf;
+ else
+ free(buf);
return file;
}
return ret;
}
-const char *
+char *
test_get_path(const char *path_rel)
{
- static char path[PATH_MAX];
+ char *path;
+ size_t path_len;
const char *srcdir = getenv("srcdir");
- snprintf(path, PATH_MAX - 1,
+ path_len = strlen(srcdir ? srcdir : ".") +
+ strlen(path_rel ? path_rel : "") + 12;
+ path = malloc(path_len);
+ if (!path) {
+ fprintf(stderr, "Failed to allocate path (%d chars) for %s\n",
+ (int) path_len, path);
+ return NULL;
+ }
+ snprintf(path, path_len,
"%s/test/data/%s", srcdir ? srcdir : ".",
path_rel ? path_rel : "");
test_read_file(const char *path_rel)
{
struct stat info;
- char *ret, *tmp;
+ char *ret, *tmp, *path;
int fd, count, remaining;
- fd = open(test_get_path(path_rel), O_RDONLY);
+ path = test_get_path(path_rel);
+ if (!path)
+ return NULL;
+
+ fd = open(path, O_RDONLY);
+ free(path);
if (fd < 0)
return NULL;
{
enum xkb_context_flags ctx_flags;
struct xkb_context *ctx;
+ char *path;
ctx_flags = XKB_CONTEXT_NO_DEFAULT_INCLUDES;
if (test_flags & CONTEXT_ALLOW_ENVIRONMENT_NAMES) {
if (!ctx)
return NULL;
- xkb_context_include_path_append(ctx, test_get_path(""));
+ path = test_get_path("");
+ if (!path)
+ return NULL;
+
+ xkb_context_include_path_append(ctx, path);
+ free(path);
return ctx;
}
{
struct xkb_keymap *keymap;
FILE *file;
- const char *path = test_get_path(path_rel);
+ char *path;
+
+ path = test_get_path(path_rel);
+ if (!path)
+ return NULL;
file = fopen(path, "r");
if (!file) {
fprintf(stderr, "Failed to open path: %s\n", path);
+ free(path);
return NULL;
}
assert(file != NULL);
if (!keymap) {
fprintf(stderr, "Failed to compile path: %s\n", path);
+ free(path);
return NULL;
}
fprintf(stderr, "Successfully compiled path: %s\n", path);
+ free(path);
return keymap;
}