Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changes/api/+reject-unsupported-flags.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fixed some functions accepting unsupported flags:
- `xkb_context_new()`
- `xkb_rmlvo_builder_new()`
- `xkb_compose_state_new()`
- `rxkb_context_new()`
13 changes: 11 additions & 2 deletions src/compose/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <assert.h>

#include "context.h"
#include "table.h"
#include "utils.h"
#include "keysym.h"
Expand All @@ -33,9 +34,17 @@ struct xkb_compose_state *
xkb_compose_state_new(struct xkb_compose_table *table,
enum xkb_compose_state_flags flags)
{
struct xkb_compose_state *state;
static const enum xkb_compose_state_flags XKB_COMPOSE_STATE_FLAGS =
XKB_COMPOSE_STATE_NO_FLAGS;

state = calloc(1, sizeof(*state));
if (flags & ~XKB_COMPOSE_STATE_FLAGS) {
log_err_func(table->ctx, XKB_LOG_MESSAGE_NO_ID,
"Unsupported compose state flags: %#x\n",
(flags & ~XKB_COMPOSE_STATE_FLAGS));
return NULL;
}

struct xkb_compose_state * const state = calloc(1, sizeof(*state));
if (!state)
return NULL;

Expand Down
83 changes: 30 additions & 53 deletions src/compose/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,27 @@
#include "paths.h"

static struct xkb_compose_table *
xkb_compose_table_new(struct xkb_context *ctx,
xkb_compose_table_new(struct xkb_context *ctx, const char *func,
const char *locale,
enum xkb_compose_format format,
enum xkb_compose_compile_flags flags)
{
static const enum xkb_compose_compile_flags XKB_COMPOSE_COMPILE_FLAGS =
XKB_COMPOSE_COMPILE_NO_FLAGS;

if (flags & ~XKB_COMPOSE_COMPILE_FLAGS) {
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"%s: unrecognized flags: %#x\n", func,
(flags & ~XKB_COMPOSE_COMPILE_FLAGS));
return NULL;
}

if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"%s: unsupported compose format: %d\n", func, format);
return NULL;
}

char *resolved_locale;
struct xkb_compose_table *table;
struct compose_node dummy = {0};
Expand Down Expand Up @@ -84,27 +100,12 @@ xkb_compose_table_new_from_file(struct xkb_context *ctx,
enum xkb_compose_format format,
enum xkb_compose_compile_flags flags)
{
struct xkb_compose_table *table;
bool ok;

if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unrecognized flags: %#x\n", flags);
return NULL;
}

if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unsupported compose format: %d\n", format);
return NULL;
}

table = xkb_compose_table_new(ctx, locale, format, flags);
struct xkb_compose_table * const table =
xkb_compose_table_new(ctx, __func__, locale, format, flags);
if (!table)
return NULL;

ok = parse_file(table, file, "(unknown file)");
if (!ok) {
if (!parse_file(table, file, "(unknown file)")) {
xkb_compose_table_unref(table);
return NULL;
}
Expand All @@ -119,27 +120,12 @@ xkb_compose_table_new_from_buffer(struct xkb_context *ctx,
enum xkb_compose_format format,
enum xkb_compose_compile_flags flags)
{
struct xkb_compose_table *table;
bool ok;

if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unrecognized flags: %#x\n", flags);
return NULL;
}

if (format != XKB_COMPOSE_FORMAT_TEXT_V1) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unsupported compose format: %d\n", format);
return NULL;
}

table = xkb_compose_table_new(ctx, locale, format, flags);
struct xkb_compose_table * const table =
xkb_compose_table_new(ctx, __func__, locale, format, flags);
if (!table)
return NULL;

ok = parse_string(table, buffer, length, "(input string)");
if (!ok) {
if (!parse_string(table, buffer, length, "(input string)")) {
xkb_compose_table_unref(table);
return NULL;
}
Expand All @@ -152,24 +138,14 @@ xkb_compose_table_new_from_locale(struct xkb_context *ctx,
const char *locale,
enum xkb_compose_compile_flags flags)
{
struct xkb_compose_table *table;
char *path;
FILE *file;
bool ok;

if (flags & ~(XKB_COMPOSE_COMPILE_NO_FLAGS)) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unrecognized flags: %#x\n", flags);
return NULL;
}

table = xkb_compose_table_new(ctx, locale, XKB_COMPOSE_FORMAT_TEXT_V1,
flags);
static const enum xkb_compose_format format = XKB_COMPOSE_FORMAT_TEXT_V1;
struct xkb_compose_table * const table =
xkb_compose_table_new(ctx, __func__, locale, format, flags);
if (!table)
return NULL;

path = get_xcomposefile_path(ctx);
file = open_file(path);
char *path = get_xcomposefile_path(ctx);
FILE *file = open_file(path);
if (file)
goto found_path;
free(path);
Expand Down Expand Up @@ -199,7 +175,8 @@ xkb_compose_table_new_from_locale(struct xkb_context *ctx,
return NULL;

found_path:
ok = parse_file(table, file, path);
{} /* Label followed by a declaration is a C23 extension */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work?

found_path:
    {
        const bool ok ...
        flcose
        if (!ok) {
        }
    }

Copy link
Member Author

@wismill wismill Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will work, it’s just that I have some hope to switch to C23 for the basic features: constexpr and attributes would be great appart lifting this label restriction!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave this up to you then, whichever you prefer. all LGTM now, thanks

const bool ok = parse_file(table, file, path);
fclose(file);
if (!ok) {
free(path);
Expand Down
14 changes: 14 additions & 0 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,20 @@ xkb_context_new(enum xkb_context_flags flags)
ctx->log_fn = default_log_fn;
ctx->log_level = XKB_LOG_LEVEL_ERROR;
ctx->log_verbosity = XKB_LOG_VERBOSITY_DEFAULT;

static const enum xkb_context_flags XKB_CONTEXT_FLAGS
= XKB_CONTEXT_NO_DEFAULT_INCLUDES
| XKB_CONTEXT_NO_ENVIRONMENT_NAMES
| XKB_CONTEXT_NO_SECURE_GETENV;

if (flags & ~XKB_CONTEXT_FLAGS) {
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"Invalid context flags: 0x%x\n",
(flags & ~XKB_CONTEXT_FLAGS));
free(ctx);
return NULL;
}

ctx->use_environment_names = !(flags & XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
ctx->use_secure_getenv = !(flags & XKB_CONTEXT_NO_SECURE_GETENV);

Expand Down
15 changes: 12 additions & 3 deletions src/keymap-priv.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "xkbcommon/xkbcommon-names.h"
#include "keymap.h"
#include "messages-codes.h"

static void
update_builtin_keymap_fields(struct xkb_keymap *keymap)
Expand Down Expand Up @@ -43,13 +44,21 @@ update_builtin_keymap_fields(struct xkb_keymap *keymap)
}

struct xkb_keymap *
xkb_keymap_new(struct xkb_context *ctx,
xkb_keymap_new(struct xkb_context *ctx, const char *func,
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags)
{
struct xkb_keymap *keymap;
static const enum xkb_keymap_compile_flags XKB_KEYMAP_COMPILE_FLAGS =
XKB_KEYMAP_COMPILE_NO_FLAGS;

keymap = calloc(1, sizeof(*keymap));
if (flags & ~XKB_KEYMAP_COMPILE_FLAGS) {
log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
"%s: unrecognized keymap compilation flags: 0x%x\n", func,
(flags & ~XKB_KEYMAP_COMPILE_FLAGS));
return NULL;
}

struct xkb_keymap * const keymap = calloc(1, sizeof(*keymap));
if (!keymap)
return NULL;

Expand Down
83 changes: 24 additions & 59 deletions src/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,15 @@ xkb_keymap_new_from_rmlvo(const struct xkb_rmlvo_builder *rmlvo,
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags)
{
struct xkb_keymap *keymap;
const struct xkb_keymap_format_ops *ops;

ops = get_keymap_format_ops(format);
const struct xkb_keymap_format_ops *ops = get_keymap_format_ops(format);
if (!ops || !ops->keymap_new_from_rmlvo) {
log_err_func(rmlvo->ctx, XKB_LOG_MESSAGE_NO_ID,
"unsupported keymap format: %d\n", format);
return NULL;
}

if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
log_err_func(rmlvo->ctx, XKB_LOG_MESSAGE_NO_ID,
"unrecognized keymap compilation flags: %#x\n", flags);
return NULL;
}

keymap = xkb_keymap_new(rmlvo->ctx, format, flags);
struct xkb_keymap *keymap = xkb_keymap_new(rmlvo->ctx, __func__, format,
flags);
if (!keymap)
return NULL;

Expand All @@ -145,31 +137,20 @@ xkb_keymap_new_from_names2(struct xkb_context *ctx,
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags)
{
struct xkb_keymap *keymap;
struct xkb_rule_names rmlvo;
const struct xkb_keymap_format_ops *ops;

ops = get_keymap_format_ops(format);
const struct xkb_keymap_format_ops *ops = get_keymap_format_ops(format);
if (!ops || !ops->keymap_new_from_names) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unsupported keymap format: %d\n", format);
return NULL;
}

if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unrecognized keymap compilation flags: %#x\n", flags);
return NULL;
}

keymap = xkb_keymap_new(ctx, format, flags);
struct xkb_keymap *keymap = xkb_keymap_new(ctx, __func__, format, flags);
if (!keymap)
return NULL;

struct xkb_rule_names rmlvo = {0};
if (rmlvo_in)
rmlvo = *rmlvo_in;
else
memset(&rmlvo, 0, sizeof(rmlvo));
xkb_context_sanitize_rule_names(ctx, &rmlvo);

if (!ops->keymap_new_from_names(keymap, &rmlvo)) {
Expand Down Expand Up @@ -206,29 +187,20 @@ xkb_keymap_new_from_buffer(struct xkb_context *ctx,
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags)
{
struct xkb_keymap *keymap;
const struct xkb_keymap_format_ops *ops;

ops = get_keymap_format_ops(format);
const struct xkb_keymap_format_ops *ops = get_keymap_format_ops(format);
if (!ops || !ops->keymap_new_from_string) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unsupported keymap format: %d\n", format);
return NULL;
}

if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unrecognized keymap compilation flags: %#x\n", flags);
return NULL;
}

if (!buffer) {
log_err_func1(ctx, XKB_LOG_MESSAGE_NO_ID,
"no buffer specified\n");
return NULL;
}

keymap = xkb_keymap_new(ctx, format, flags);
struct xkb_keymap *keymap = xkb_keymap_new(ctx, __func__, format, flags);
if (!keymap)
return NULL;

Expand All @@ -250,29 +222,20 @@ xkb_keymap_new_from_file(struct xkb_context *ctx,
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags)
{
struct xkb_keymap *keymap;
const struct xkb_keymap_format_ops *ops;

ops = get_keymap_format_ops(format);
const struct xkb_keymap_format_ops *ops = get_keymap_format_ops(format);
if (!ops || !ops->keymap_new_from_file) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unsupported keymap format: %d\n", format);
return NULL;
}

if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unrecognized keymap compilation flags: %#x\n", flags);
return NULL;
}

if (!file) {
log_err_func1(ctx, XKB_LOG_MESSAGE_NO_ID,
"no file specified\n");
return NULL;
}

keymap = xkb_keymap_new(ctx, format, flags);
struct xkb_keymap *keymap = xkb_keymap_new(ctx, __func__, format, flags);
if (!keymap)
return NULL;

Expand All @@ -289,11 +252,14 @@ xkb_keymap_get_as_string2(struct xkb_keymap *keymap,
enum xkb_keymap_format format,
enum xkb_keymap_serialize_flags flags)
{
const enum xkb_keymap_serialize_flags valid_flags =
XKB_KEYMAP_SERIALIZE_PRETTY | XKB_KEYMAP_SERIALIZE_KEEP_UNUSED;
if (flags & ~valid_flags) {
static const enum xkb_keymap_serialize_flags XKB_KEYMAP_SERIALIZE_FLAGS
= XKB_KEYMAP_SERIALIZE_PRETTY
| XKB_KEYMAP_SERIALIZE_KEEP_UNUSED;

if (flags & ~XKB_KEYMAP_SERIALIZE_FLAGS) {
log_err_func(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
"unrecognized serialization flags: %#x\n", flags);
"unrecognized serialization flags: %#x\n",
(flags & ~XKB_KEYMAP_SERIALIZE_FLAGS));
return NULL;
}

Expand Down Expand Up @@ -628,19 +594,18 @@ struct xkb_keymap_key_iterator {
struct xkb_keymap *keymap;
};

enum {
XKB_KEYMAP_KEY_ITERATOR_FLAGS = XKB_KEYMAP_KEY_ITERATOR_DESCENDING_ORDER
| XKB_KEYMAP_KEY_ITERATOR_SKIP_UNBOUND
};

struct xkb_keymap_key_iterator *
xkb_keymap_key_iterator_new(struct xkb_keymap *keymap,
enum xkb_keymap_key_iterator_flags flags)
{
if (flags &
~(enum xkb_keymap_key_iterator_flags) XKB_KEYMAP_KEY_ITERATOR_FLAGS) {
static const enum xkb_keymap_key_iterator_flags XKB_KEYMAP_KEY_ITERATOR_FLAGS
= XKB_KEYMAP_KEY_ITERATOR_DESCENDING_ORDER
| XKB_KEYMAP_KEY_ITERATOR_SKIP_UNBOUND;

if (flags & ~XKB_KEYMAP_KEY_ITERATOR_FLAGS) {
log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
"unrecognized keymap iterator flags: %#x\n", flags);
"unrecognized keymap iterator flags: %#x\n",
(flags & ~XKB_KEYMAP_KEY_ITERATOR_FLAGS));
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion src/keymap.h
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ entry_is_active(const struct xkb_key_type_entry *entry)
}

struct xkb_keymap *
xkb_keymap_new(struct xkb_context *ctx,
xkb_keymap_new(struct xkb_context *ctx, const char* func,
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags);

Expand Down
Loading
Loading