From e4fcbef89a37a521552ac95a35af1bed0e131ec4 Mon Sep 17 00:00:00 2001 From: Demi Marie Obenour Date: Sat, 7 Jun 2025 15:21:37 -0400 Subject: [PATCH] pod: Improve type-safety in SPA POD code Use direct field access when the type is known, instead of a macro that includes a cast. --- spa/include/spa/debug/pod.h | 4 +--- spa/include/spa/pod/compare.h | 14 +++++++------- spa/include/spa/pod/filter.h | 10 +++++----- spa/include/spa/pod/pod.h | 8 ++++---- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/spa/include/spa/debug/pod.h b/spa/include/spa/debug/pod.h index fe425676d..1dd1a86b9 100644 --- a/spa/include/spa/debug/pod.h +++ b/spa/include/spa/debug/pod.h @@ -186,9 +186,7 @@ SPA_API_DEBUG_POD int spa_debugc_pod(struct spa_debug_context *ctx, int indent, const struct spa_type_info *info, const struct spa_pod *pod) { return spa_debugc_pod_value(ctx, indent, info ? info : SPA_TYPE_ROOT, - SPA_POD_TYPE(pod), - SPA_POD_BODY(pod), - SPA_POD_BODY_SIZE(pod)); + pod->type, SPA_POD_BODY(pod), pod->size); } SPA_API_DEBUG_POD int diff --git a/spa/include/spa/pod/compare.h b/spa/include/spa/pod/compare.h index 12c65fd44..96de89d24 100644 --- a/spa/include/spa/pod/compare.h +++ b/spa/include/spa/pod/compare.h @@ -94,10 +94,10 @@ SPA_API_POD_COMPARE int spa_pod_compare(const struct spa_pod *pod1, if (n_vals1 != n_vals2) return -EINVAL; - if (SPA_POD_TYPE(pod1) != SPA_POD_TYPE(pod2)) + if (pod1->type != pod2->type) return -EINVAL; - switch (SPA_POD_TYPE(pod1)) { + switch (pod1->type) { case SPA_TYPE_Struct: { const struct spa_pod *p1, *p2; @@ -145,17 +145,17 @@ SPA_API_POD_COMPARE int spa_pod_compare(const struct spa_pod *pod1, } case SPA_TYPE_Array: { - if (SPA_POD_BODY_SIZE(pod1) != SPA_POD_BODY_SIZE(pod2)) + if (pod1->size != pod2->size) return -EINVAL; - res = memcmp(SPA_POD_BODY(pod1), SPA_POD_BODY(pod2), SPA_POD_BODY_SIZE(pod2)); + res = memcmp(SPA_POD_BODY(pod1), SPA_POD_BODY(pod2), pod2->size); break; } default: - if (SPA_POD_BODY_SIZE(pod1) != SPA_POD_BODY_SIZE(pod2)) + if (pod1->size != pod2->size) return -EINVAL; - res = spa_pod_compare_value(SPA_POD_TYPE(pod1), + res = spa_pod_compare_value(pod1->type, SPA_POD_BODY(pod1), SPA_POD_BODY(pod2), - SPA_POD_BODY_SIZE(pod1)); + pod1->size); break; } return res; diff --git a/spa/include/spa/pod/filter.h b/spa/include/spa/pod/filter.h index 5f94683fd..699bddc27 100644 --- a/spa/include/spa/pod/filter.h +++ b/spa/include/spa/pod/filter.h @@ -262,14 +262,14 @@ SPA_API_POD_FILTER int spa_pod_filter_part(struct spa_pod_builder *b, uint32_t filter_offset = 0; struct spa_pod_frame f; - switch (SPA_POD_TYPE(pp)) { + switch (pp->type) { case SPA_TYPE_Object: if (pf != NULL) { struct spa_pod_object *op = (struct spa_pod_object *) pp; struct spa_pod_object *of = (struct spa_pod_object *) pf; const struct spa_pod_prop *p1, *p2; - if (SPA_POD_TYPE(pf) != SPA_POD_TYPE(pp)) + if (pf->type != pp->type) return -EINVAL; spa_pod_builder_push_object(b, &f, op->body.type, op->body.id); @@ -307,7 +307,7 @@ SPA_API_POD_FILTER int spa_pod_filter_part(struct spa_pod_builder *b, case SPA_TYPE_Struct: if (pf != NULL) { - if (SPA_POD_TYPE(pf) != SPA_POD_TYPE(pp)) + if (pf->type != pp->type) return -EINVAL; filter_offset = sizeof(struct spa_pod_struct); @@ -326,9 +326,9 @@ SPA_API_POD_FILTER int spa_pod_filter_part(struct spa_pod_builder *b, default: if (pf != NULL) { - if (SPA_POD_SIZE(pp) != SPA_POD_SIZE(pf)) + if (pp->size != pf->size) return -EINVAL; - if (memcmp(pp, pf, SPA_POD_SIZE(pp)) != 0) + if (memcmp(pp, pf, pp->size) != 0) return -EINVAL; do_advance = true; } diff --git a/spa/include/spa/pod/pod.h b/spa/include/spa/pod/pod.h index 854a65abc..f2b5c766e 100644 --- a/spa/include/spa/pod/pod.h +++ b/spa/include/spa/pod/pod.h @@ -96,8 +96,8 @@ struct spa_pod_bitmap { }; #define SPA_POD_ARRAY_CHILD(arr) (&((struct spa_pod_array*)(arr))->body.child) -#define SPA_POD_ARRAY_VALUE_TYPE(arr) (SPA_POD_TYPE(SPA_POD_ARRAY_CHILD(arr))) -#define SPA_POD_ARRAY_VALUE_SIZE(arr) (SPA_POD_BODY_SIZE(SPA_POD_ARRAY_CHILD(arr))) +#define SPA_POD_ARRAY_VALUE_TYPE(arr) (SPA_POD_ARRAY_CHILD(arr)->type) +#define SPA_POD_ARRAY_VALUE_SIZE(arr) (SPA_POD_ARRAY_CHILD(arr)->size) #define SPA_POD_ARRAY_N_VALUES(arr) (SPA_POD_ARRAY_VALUE_SIZE(arr) ? ((SPA_POD_BODY_SIZE(arr) - sizeof(struct spa_pod_array_body)) / SPA_POD_ARRAY_VALUE_SIZE(arr)) : 0) #define SPA_POD_ARRAY_VALUES(arr) SPA_POD_CONTENTS(struct spa_pod_array, arr) @@ -114,8 +114,8 @@ struct spa_pod_array { #define SPA_POD_CHOICE_CHILD(choice) (&((struct spa_pod_choice*)(choice))->body.child) #define SPA_POD_CHOICE_TYPE(choice) (((struct spa_pod_choice*)(choice))->body.type) #define SPA_POD_CHOICE_FLAGS(choice) (((struct spa_pod_choice*)(choice))->body.flags) -#define SPA_POD_CHOICE_VALUE_TYPE(choice) (SPA_POD_TYPE(SPA_POD_CHOICE_CHILD(choice))) -#define SPA_POD_CHOICE_VALUE_SIZE(choice) (SPA_POD_BODY_SIZE(SPA_POD_CHOICE_CHILD(choice))) +#define SPA_POD_CHOICE_VALUE_TYPE(choice) (SPA_POD_CHOICE_CHILD(choice)->type) +#define SPA_POD_CHOICE_VALUE_SIZE(choice) (SPA_POD_CHOICE_CHILD(choice)->size) #define SPA_POD_CHOICE_N_VALUES(choice) (SPA_POD_CHOICE_VALUE_SIZE(choice) ? ((SPA_POD_BODY_SIZE(choice) - sizeof(struct spa_pod_choice_body)) / SPA_POD_CHOICE_VALUE_SIZE(choice)) : 0) #define SPA_POD_CHOICE_VALUES(choice) (SPA_POD_CONTENTS(struct spa_pod_choice, choice))