diff options
author | Uli Schlachter <psychon@znc.in> | 2015-04-05 14:37:24 +0200 |
---|---|---|
committer | Uli Schlachter <psychon@znc.in> | 2015-04-05 14:37:24 +0200 |
commit | 861fc2c169cad96056de3564c4d7550433285744 (patch) | |
tree | 043730b0e4447507428fe847c06d38c3909dfaf8 | |
parent | 64fa1e695b528272cb38c4c2cbc0e296c8fca57d (diff) |
Handle NULL as context argument
Signed-off-by: Uli Schlachter <psychon@znc.in>
-rw-r--r-- | src/xcb_errors.c | 48 | ||||
-rw-r--r-- | tests/test.c | 23 |
2 files changed, 61 insertions, 10 deletions
diff --git a/src/xcb_errors.c b/src/xcb_errors.c index b796ae4..8e90eee 100644 --- a/src/xcb_errors.c +++ b/src/xcb_errors.c @@ -28,6 +28,11 @@ #include <stdlib.h> #include <string.h> +#define CHECK_CONTEXT(ctx) do { \ + if ((ctx) == NULL) \ + return "xcb-errors API misuse: context argument is NULL"; \ +} while (0) + struct extension_info_t { struct extension_info_t *next; const struct static_extension_info_t *static_info; @@ -118,8 +123,11 @@ void xcb_errors_context_free(xcb_errors_context_t *ctx) const char *xcb_errors_get_name_for_major_code(xcb_errors_context_t *ctx, uint8_t major_code) { - struct extension_info_t *info = ctx->extensions; + struct extension_info_t *info; + + CHECK_CONTEXT(ctx); + info = ctx->extensions; while (info && info->major_opcode != major_code) info = info->next; @@ -133,8 +141,11 @@ const char *xcb_errors_get_name_for_minor_code(xcb_errors_context_t *ctx, uint8_t major_code, uint16_t minor_code) { - struct extension_info_t *info = ctx->extensions; + struct extension_info_t *info; + + CHECK_CONTEXT(ctx); + info = ctx->extensions; while (info && info->major_opcode != major_code) info = info->next; @@ -147,8 +158,11 @@ const char *xcb_errors_get_name_for_minor_code(xcb_errors_context_t *ctx, const char *xcb_errors_get_name_for_xge_event(xcb_errors_context_t *ctx, uint8_t major_code, uint16_t event_type) { - struct extension_info_t *info = ctx->extensions; + struct extension_info_t *info; + + CHECK_CONTEXT(ctx); + info = ctx->extensions; while (info && info->major_opcode != major_code) info = info->next; @@ -162,12 +176,18 @@ const char *xcb_errors_get_name_for_event(xcb_errors_context_t *ctx, uint8_t event_code, const char **extension) { struct extension_info_t *best = NULL; - struct extension_info_t *next = ctx->extensions; + struct extension_info_t *next; + + if (extension) + *extension = NULL; + + CHECK_CONTEXT(ctx); /* Find the extension with the largest first_event <= event_code. Thanks * to this we do the right thing if the server only supports an older * version of some extension which had less events. */ + next = ctx->extensions; while (next) { struct extension_info_t *current = next; next = next->next; @@ -181,8 +201,6 @@ const char *xcb_errors_get_name_for_event(xcb_errors_context_t *ctx, if (best == NULL || best->first_event == 0) { /* Nothing found */ - if (extension) - *extension = NULL; return get_strings_entry(xproto_info.strings_events, event_code); } @@ -195,12 +213,18 @@ const char *xcb_errors_get_name_for_error(xcb_errors_context_t *ctx, uint8_t error_code, const char **extension) { struct extension_info_t *best = NULL; - struct extension_info_t *next = ctx->extensions; + struct extension_info_t *next; + + if (extension) + *extension = NULL; + + CHECK_CONTEXT(ctx); /* Find the extension with the largest first_error <= error_code. Thanks * to this we do the right thing if the server only supports an older * version of some extension which had less events. */ + next = ctx->extensions; while (next) { struct extension_info_t *current = next; next = next->next; @@ -214,8 +238,6 @@ const char *xcb_errors_get_name_for_error(xcb_errors_context_t *ctx, if (best == NULL || best->first_error == 0) { /* Nothing found */ - if (extension) - *extension = NULL; return get_strings_entry(xproto_info.strings_errors, error_code); } @@ -227,9 +249,15 @@ const char *xcb_errors_get_name_for_error(xcb_errors_context_t *ctx, const char *xcb_errors_get_name_for_xcb_event(xcb_errors_context_t *ctx, xcb_generic_event_t *event, const char **extension) { - struct extension_info_t *xkb = ctx->extensions; + struct extension_info_t *xkb; + + if (extension) + *extension = NULL; + + CHECK_CONTEXT(ctx); /* Find the xkb extension, if present */ + xkb = ctx->extensions; while (xkb != NULL && strcmp(xkb->static_info->name, "xkb") != 0) xkb = xkb->next; diff --git a/tests/test.c b/tests/test.c index 15f1f4f..85519d3 100644 --- a/tests/test.c +++ b/tests/test.c @@ -351,10 +351,33 @@ static int test_valid_connection(void) return err; } +static int test_NULL_context(void) +{ + int err = 0; + const char *msg = "xcb-errors API misuse: context argument is NULL"; + + xcb_errors_context_free(NULL); + err |= check_strings(msg, xcb_errors_get_name_for_major_code(NULL, 0), + "xcb_errors_get_name_for_major_code(NULL, 0) does not behave correctly"); + err |= check_strings(msg, xcb_errors_get_name_for_minor_code(NULL, 0, 0), + "xcb_errors_get_name_for_minor_code(NULL, 0, 0) does not behave correctly"); + err |= check_strings(msg, xcb_errors_get_name_for_event(NULL, 0, NULL), + "xcb_errors_get_name_for_event(NULL, 0, NULL) does not behave correctly"); + err |= check_strings(msg, xcb_errors_get_name_for_xge_event(NULL, 0, 0), + "xcb_errors_get_name_for_xge_event(NULL, 0, 0) does not behave correctly"); + err |= check_strings(msg, xcb_errors_get_name_for_xcb_event(NULL, NULL, NULL), + "xcb_errors_get_name_for_xcb_event(NULL, NULL, NULL) does not behave correctly"); + err |= check_strings(msg, xcb_errors_get_name_for_error(NULL, 0, NULL), + "xcb_errors_get_name_for_xcb_error(NULL, 0, NULL) does not behave correctly"); + + return err; +} + int main(void) { int err = 0; err |= test_error_connection(); err |= test_valid_connection(); + err |= test_NULL_context(); return err; } |