Skip to content

Commit 97472f2

Browse files
committed
Review fixes
1 parent f2c3c50 commit 97472f2

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

src/compose/table.c

+12-20
Original file line numberDiff line numberDiff line change
@@ -176,35 +176,27 @@ xkb_compose_table_new_from_locale(struct xkb_context *ctx,
176176
return NULL;
177177

178178
path = get_xcomposefile_path(ctx);
179-
if (path && is_regular_file(path)) {
180-
file = fopen(path, "rb");
181-
if (file)
182-
goto found_path;
183-
}
179+
file = open_regular_file(path);
180+
if (file)
181+
goto found_path;
184182
free(path);
185183

186184
path = get_xdg_xcompose_file_path(ctx);
187-
if (path && is_regular_file(path)) {
188-
file = fopen(path, "rb");
189-
if (file)
190-
goto found_path;
191-
}
185+
file = open_regular_file(path);
186+
if (file)
187+
goto found_path;
192188
free(path);
193189

194190
path = get_home_xcompose_file_path(ctx);
195-
if (path && is_regular_file(path)) {
196-
file = fopen(path, "rb");
197-
if (file)
198-
goto found_path;
199-
}
191+
file = open_regular_file(path);
192+
if (file)
193+
goto found_path;
200194
free(path);
201195

202196
path = get_locale_compose_file_path(ctx, table->locale);
203-
if (path && is_regular_file(path)) {
204-
file = fopen(path, "rb");
205-
if (file)
206-
goto found_path;
207-
}
197+
file = open_regular_file(path);
198+
if (file)
199+
goto found_path;
208200
free(path);
209201

210202
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,

src/utils.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
#include "config.h"
2525

2626
#include <sys/stat.h>
27+
#include <fcntl.h>
2728
#include "utils.h"
2829

2930
#ifdef HAVE_MMAP
3031

31-
#include <fcntl.h>
3232
#include <unistd.h>
3333
#include <sys/mman.h>
3434
#include <sys/types.h>
@@ -111,14 +111,21 @@ unmap_file(char *str, size_t size)
111111

112112
#endif
113113

114-
bool
115-
is_regular_file(const char *path)
114+
FILE*
115+
open_regular_file(const char *path)
116116
{
117+
if (!path) return NULL;
118+
119+
int fd = open(path, O_RDONLY);
117120
struct stat stat_buf;
118-
int err = stat(path, &stat_buf);
119-
if (err != 0) return false;
121+
int err = fstat(fd, &stat_buf);
122+
123+
if (err != 0 || !S_ISREG(stat_buf.st_mode)) {
124+
close(fd);
125+
return NULL;
126+
}
120127

121-
return S_ISREG(stat_buf.st_mode);
128+
return fdopen(fd, "rb");
122129
}
123130

124131
// ASCII lower-case map.

src/utils.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ check_eaccess(const char *path, int mode)
266266
return true;
267267
}
268268

269-
bool
270-
is_regular_file(const char *path);
269+
FILE*
270+
open_regular_file(const char *path);
271271

272272
#if defined(HAVE_SECURE_GETENV)
273273
# define secure_getenv secure_getenv

0 commit comments

Comments
 (0)