Skip to content

Commit f17047f

Browse files
committed
compose: Reject unsupported compile flags
1 parent 46c2655 commit f17047f

File tree

2 files changed

+51
-53
lines changed

2 files changed

+51
-53
lines changed

src/compose/table.c

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,27 @@
1616
#include "paths.h"
1717

1818
static struct xkb_compose_table *
19-
xkb_compose_table_new(struct xkb_context *ctx,
19+
xkb_compose_table_new(struct xkb_context *ctx, const char *func,
2020
const char *locale,
2121
enum xkb_compose_format format,
2222
enum xkb_compose_compile_flags flags)
2323
{
24+
static const enum xkb_compose_compile_flags XKB_COMPOSE_COMPILE_FLAGS =
25+
XKB_COMPOSE_COMPILE_NO_FLAGS;
26+
27+
if (flags & ~XKB_COMPOSE_COMPILE_FLAGS) {
28+
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
29+
"%s: unrecognized flags: %#x\n", func,
30+
(flags & ~XKB_COMPOSE_COMPILE_FLAGS));
31+
return NULL;
32+
}
33+
34+
if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
35+
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
36+
"%s: unsupported compose format: %d\n", func, format);
37+
return NULL;
38+
}
39+
2440
char *resolved_locale;
2541
struct xkb_compose_table *table;
2642
struct compose_node dummy = {0};
@@ -84,27 +100,12 @@ xkb_compose_table_new_from_file(struct xkb_context *ctx,
84100
enum xkb_compose_format format,
85101
enum xkb_compose_compile_flags flags)
86102
{
87-
struct xkb_compose_table *table;
88-
bool ok;
89-
90-
if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
91-
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
92-
"unrecognized flags: %#x\n", flags);
93-
return NULL;
94-
}
95-
96-
if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
97-
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
98-
"unsupported compose format: %d\n", format);
99-
return NULL;
100-
}
101-
102-
table = xkb_compose_table_new(ctx, locale, format, flags);
103+
struct xkb_compose_table * const table =
104+
xkb_compose_table_new(ctx, __func__, locale, format, flags);
103105
if (!table)
104106
return NULL;
105107

106-
ok = parse_file(table, file, "(unknown file)");
107-
if (!ok) {
108+
if (!parse_file(table, file, "(unknown file)")) {
108109
xkb_compose_table_unref(table);
109110
return NULL;
110111
}
@@ -119,27 +120,12 @@ xkb_compose_table_new_from_buffer(struct xkb_context *ctx,
119120
enum xkb_compose_format format,
120121
enum xkb_compose_compile_flags flags)
121122
{
122-
struct xkb_compose_table *table;
123-
bool ok;
124-
125-
if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
126-
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
127-
"unrecognized flags: %#x\n", flags);
128-
return NULL;
129-
}
130-
131-
if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
132-
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
133-
"unsupported compose format: %d\n", format);
134-
return NULL;
135-
}
136-
137-
table = xkb_compose_table_new(ctx, locale, format, flags);
123+
struct xkb_compose_table * const table =
124+
xkb_compose_table_new(ctx, __func__, locale, format, flags);
138125
if (!table)
139126
return NULL;
140127

141-
ok = parse_string(table, buffer, length, "(input string)");
142-
if (!ok) {
128+
if (!parse_string(table, buffer, length, "(input string)")) {
143129
xkb_compose_table_unref(table);
144130
return NULL;
145131
}
@@ -152,24 +138,14 @@ xkb_compose_table_new_from_locale(struct xkb_context *ctx,
152138
const char *locale,
153139
enum xkb_compose_compile_flags flags)
154140
{
155-
struct xkb_compose_table *table;
156-
char *path;
157-
FILE *file;
158-
bool ok;
159-
160-
if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
161-
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
162-
"unrecognized flags: %#x\n", flags);
163-
return NULL;
164-
}
165-
166-
table = xkb_compose_table_new(ctx, locale, XKB_COMPOSE_FORMAT_TEXT_V1,
167-
flags);
141+
static const enum xkb_compose_format format = XKB_COMPOSE_FORMAT_TEXT_V1;
142+
struct xkb_compose_table * const table =
143+
xkb_compose_table_new(ctx, __func__, locale, format, flags);
168144
if (!table)
169145
return NULL;
170146

171-
path = get_xcomposefile_path(ctx);
172-
file = open_file(path);
147+
char *path = get_xcomposefile_path(ctx);
148+
FILE *file = open_file(path);
173149
if (file)
174150
goto found_path;
175151
free(path);
@@ -199,7 +175,8 @@ xkb_compose_table_new_from_locale(struct xkb_context *ctx,
199175
return NULL;
200176

201177
found_path:
202-
ok = parse_file(table, file, path);
178+
{} /* Label followed by a declaration is a C23 extension */
179+
const bool ok = parse_file(table, file, path);
203180
fclose(file);
204181
if (!ok) {
205182
free(path);

test/compose.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ static void
174174
test_compose_utf8_bom(struct xkb_context *ctx)
175175
{
176176
const char buffer[] = "\xef\xbb\xbf<A> : X";
177+
178+
/* Reject invalid flags */
179+
assert(!xkb_compose_table_new_from_buffer(ctx,
180+
buffer, sizeof(buffer), "",
181+
XKB_COMPOSE_FORMAT_TEXT_V1,
182+
-1));
183+
assert(!xkb_compose_table_new_from_buffer(ctx,
184+
buffer, sizeof(buffer), "",
185+
XKB_COMPOSE_FORMAT_TEXT_V1,
186+
0xffff));
187+
177188
assert(test_compose_seq_buffer(ctx, buffer,
178189
XKB_KEY_A, XKB_COMPOSE_FEED_ACCEPTED, XKB_COMPOSE_COMPOSED, "X", XKB_KEY_X,
179190
XKB_KEY_NoSymbol));
@@ -464,6 +475,12 @@ test_state(struct xkb_context *ctx)
464475
assert(file);
465476
free(path);
466477

478+
/* Reject invalid flags */
479+
assert(!xkb_compose_table_new_from_file(ctx, file, "",
480+
XKB_COMPOSE_FORMAT_TEXT_V1, -1));
481+
assert(!xkb_compose_table_new_from_file(ctx, file, "",
482+
XKB_COMPOSE_FORMAT_TEXT_V1, 0xffff));
483+
467484
table = xkb_compose_table_new_from_file(ctx, file, "",
468485
XKB_COMPOSE_FORMAT_TEXT_V1,
469486
XKB_COMPOSE_COMPILE_NO_FLAGS);
@@ -556,6 +573,10 @@ test_from_locale(struct xkb_context *ctx)
556573
setenv("XLOCALEDIR", path, 1);
557574
free(path);
558575

576+
/* Reject invalid flags */
577+
assert(!xkb_compose_table_new_from_locale(ctx, "en_US.UTF-8", -1));
578+
assert(!xkb_compose_table_new_from_locale(ctx, "en_US.UTF-8", 0xffff));
579+
559580
/* Direct directory name match. */
560581
table = xkb_compose_table_new_from_locale(ctx, "en_US.UTF-8",
561582
XKB_COMPOSE_COMPILE_NO_FLAGS);

0 commit comments

Comments
 (0)