From 82aff583b7f7e018ad4a1db92dc635df8e5ebe7b Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Wed, 4 Dec 2019 12:45:42 +1300 Subject: [PATCH] libndr: Return enum ndr_err_code from ndr_{pull,push}_steal_switch_value() This breaks the ABI so we merge this into the unreleased libndr-1.0.0. The advantage of the new functions is there (except for print, which is unchanged) is an error raised when the token is not found, so we can be confident in the changes to the token behaviour. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13876 Signed-off-by: Andrew Bartlett Reviewed-by: Gary Lockyer Autobuild-User(master): Andrew Bartlett Autobuild-Date(master): Thu Dec 12 03:56:23 UTC 2019 on sn-devel-184 --- librpc/ABI/ndr-1.0.0.sigs | 4 ++-- librpc/ndr/libndr.h | 11 ++++++++-- librpc/ndr/ndr.c | 28 +++++++----------------- librpc/ndr/ndr_drsuapi.c | 6 +++-- pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm | 8 +++---- 5 files changed, 27 insertions(+), 30 deletions(-) diff --git a/librpc/ABI/ndr-1.0.0.sigs b/librpc/ABI/ndr-1.0.0.sigs index 3177f3804d4..bc7c3e88848 100644 --- a/librpc/ABI/ndr-1.0.0.sigs +++ b/librpc/ABI/ndr-1.0.0.sigs @@ -138,7 +138,7 @@ ndr_pull_restore_relative_base_offset: void (struct ndr_pull *, uint32_t) ndr_pull_set_switch_value: enum ndr_err_code (struct ndr_pull *, const void *, uint32_t) ndr_pull_setup_relative_base_offset1: enum ndr_err_code (struct ndr_pull *, const void *, uint32_t) ndr_pull_setup_relative_base_offset2: enum ndr_err_code (struct ndr_pull *, const void *) -ndr_pull_steal_switch_value: uint32_t (struct ndr_pull *, const void *) +ndr_pull_steal_switch_value: enum ndr_err_code (struct ndr_pull *, const void *, uint32_t *) ndr_pull_string: enum ndr_err_code (struct ndr_pull *, int, const char **) ndr_pull_string_array: enum ndr_err_code (struct ndr_pull *, int, const char ***) ndr_pull_struct_blob: enum ndr_err_code (const DATA_BLOB *, TALLOC_CTX *, void *, ndr_pull_flags_fn_t) @@ -213,7 +213,7 @@ ndr_push_setup_relative_base_offset1: enum ndr_err_code (struct ndr_push *, cons ndr_push_setup_relative_base_offset2: enum ndr_err_code (struct ndr_push *, const void *) ndr_push_short_relative_ptr1: enum ndr_err_code (struct ndr_push *, const void *) ndr_push_short_relative_ptr2: enum ndr_err_code (struct ndr_push *, const void *) -ndr_push_steal_switch_value: uint32_t (struct ndr_push *, const void *) +ndr_push_steal_switch_value: enum ndr_err_code (struct ndr_push *, const void *, uint32_t *) ndr_push_string: enum ndr_err_code (struct ndr_push *, int, const char *) ndr_push_string_array: enum ndr_err_code (struct ndr_push *, int, const char **) ndr_push_struct_blob: enum ndr_err_code (DATA_BLOB *, TALLOC_CTX *, const void *, ndr_push_flags_fn_t) diff --git a/librpc/ndr/libndr.h b/librpc/ndr/libndr.h index 2a4e3fb098a..58ef517d363 100644 --- a/librpc/ndr/libndr.h +++ b/librpc/ndr/libndr.h @@ -616,9 +616,16 @@ enum ndr_err_code ndr_check_pipe_chunk_trailer(struct ndr_pull *ndr, int ndr_fla enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val); enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val); enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val); -uint32_t ndr_push_steal_switch_value(struct ndr_push *ndr, const void *p); +/* retrieve a switch value (for push) and remove it from the list */ +enum ndr_err_code ndr_push_steal_switch_value(struct ndr_push *ndr, + const void *p, + uint32_t *v); +/* retrieve a switch value and remove it from the list */ uint32_t ndr_print_steal_switch_value(struct ndr_print *ndr, const void *p); -uint32_t ndr_pull_steal_switch_value(struct ndr_pull *ndr, const void *p); +/* retrieve a switch value and remove it from the list */ +enum ndr_err_code ndr_pull_steal_switch_value(struct ndr_pull *ndr, + const void *p, + uint32_t *v); enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p, ndr_pull_flags_fn_t fn); enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p, ndr_pull_flags_fn_t fn); enum ndr_err_code ndr_pull_struct_blob_all_noalloc(const DATA_BLOB *blob, diff --git a/librpc/ndr/ndr.c b/librpc/ndr/ndr.c index 5616eec211f..2259a35b170 100644 --- a/librpc/ndr/ndr.c +++ b/librpc/ndr/ndr.c @@ -1231,17 +1231,11 @@ _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, con } /* retrieve a switch value (for push) and remove it from the list */ -_PUBLIC_ uint32_t ndr_push_steal_switch_value(struct ndr_push *ndr, const void *p) +_PUBLIC_ enum ndr_err_code ndr_push_steal_switch_value(struct ndr_push *ndr, + const void *p, + uint32_t *v) { - enum ndr_err_code status; - uint32_t v; - - status = ndr_token_retrieve(&ndr->switch_list, p, &v); - if (!NDR_ERR_CODE_IS_SUCCESS(status)) { - return 0; - } - - return v; + return ndr_token_retrieve(&ndr->switch_list, p, v); } /* retrieve a switch value and remove it from the list */ @@ -1259,17 +1253,11 @@ _PUBLIC_ uint32_t ndr_print_steal_switch_value(struct ndr_print *ndr, const void } /* retrieve a switch value and remove it from the list */ -_PUBLIC_ uint32_t ndr_pull_steal_switch_value(struct ndr_pull *ndr, const void *p) +_PUBLIC_ enum ndr_err_code ndr_pull_steal_switch_value(struct ndr_pull *ndr, + const void *p, + uint32_t *v) { - enum ndr_err_code status; - uint32_t v; - - status = ndr_token_retrieve(&ndr->switch_list, p, &v); - if (!NDR_ERR_CODE_IS_SUCCESS(status)) { - return 0; - } - - return v; + return ndr_token_retrieve(&ndr->switch_list, p, v); } /* diff --git a/librpc/ndr/ndr_drsuapi.c b/librpc/ndr/ndr_drsuapi.c index a0a99ef3663..b03c1ec57b3 100644 --- a/librpc/ndr/ndr_drsuapi.c +++ b/librpc/ndr/ndr_drsuapi.c @@ -420,7 +420,8 @@ enum ndr_err_code ndr_push_drsuapi_DsBindInfo(struct ndr_push *ndr, int ndr_flag ndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64; NDR_PUSH_CHECK_FLAGS(ndr, ndr_flags); if (ndr_flags & NDR_SCALARS) { - uint32_t level = ndr_push_steal_switch_value(ndr, r); + uint32_t level; + NDR_CHECK(ndr_push_steal_switch_value(ndr, r, &level)); NDR_CHECK(ndr_push_union_align(ndr, 4)); switch (level) { case 24: { @@ -480,7 +481,8 @@ enum ndr_err_code ndr_pull_drsuapi_DsBindInfo(struct ndr_pull *ndr, int ndr_flag ndr->flags = ndr->flags & ~LIBNDR_FLAG_NDR64; NDR_PULL_CHECK_FLAGS(ndr, ndr_flags); if (ndr_flags & NDR_SCALARS) { - uint32_t level = ndr_pull_steal_switch_value(ndr, r); + uint32_t level; + NDR_CHECK(ndr_pull_steal_switch_value(ndr, r, &level)); NDR_CHECK(ndr_pull_union_align(ndr, 4)); switch (level) { case 24: { diff --git a/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm b/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm index 31dce4bc2ba..0d58cb5f03d 100644 --- a/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm +++ b/pidl/lib/Parse/Pidl/Samba4/NDR/Parser.pm @@ -1983,7 +1983,7 @@ sub ParseUnionPush($$$$) $self->pidl("if (ndr_flags & NDR_SCALARS) {"); $self->indent; $self->pidl("/* This token is not used again (except perhaps below in the NDR_BUFFERS case) */"); - $self->pidl("level = ndr_push_steal_switch_value($ndr, $varname);"); + $self->pidl("NDR_CHECK(ndr_push_steal_switch_value($ndr, $varname, &level));"); $self->ParseUnionPushPrimitives($e, $ndr, $varname); $self->deindent; @@ -1995,7 +1995,7 @@ sub ParseUnionPush($$$$) $self->pidl("if (!(ndr_flags & NDR_SCALARS)) {"); $self->indent; $self->pidl("/* We didn't get it above, and the token is not needed after this. */"); - $self->pidl("level = ndr_push_steal_switch_value($ndr, $varname);"); + $self->pidl("NDR_CHECK(ndr_push_steal_switch_value($ndr, $varname, &level));"); $self->deindent; $self->pidl("}"); $self->ParseUnionPushDeferred($e, $ndr, $varname); @@ -2171,7 +2171,7 @@ sub ParseUnionPull($$$$) $self->pidl("if (ndr_flags & NDR_SCALARS) {"); $self->indent; $self->pidl("/* This token is not used again (except perhaps below in the NDR_BUFFERS case) */"); - $self->pidl("level = ndr_pull_steal_switch_value($ndr, $varname);"); + $self->pidl("NDR_CHECK(ndr_pull_steal_switch_value($ndr, $varname, &level));"); $self->ParseUnionPullPrimitives($e,$ndr,$varname,$switch_type); $self->deindent; $self->pidl("}"); @@ -2182,7 +2182,7 @@ sub ParseUnionPull($$$$) $self->pidl("if (!(ndr_flags & NDR_SCALARS)) {"); $self->indent; $self->pidl("/* We didn't get it above, and the token is not needed after this. */"); - $self->pidl("level = ndr_pull_steal_switch_value($ndr, $varname);"); + $self->pidl("NDR_CHECK(ndr_pull_steal_switch_value($ndr, $varname, &level));"); $self->deindent; $self->pidl("}"); $self->ParseUnionPullDeferred($e,$ndr,$varname);