From 7d41876fcfde17c73258411e5032d046fcefeac2 Mon Sep 17 00:00:00 2001 From: Eric Devolder Date: Sat, 17 Feb 2024 13:18:10 +0100 Subject: [PATCH] fixing issue with comparison on some platforms (MIPS) (#55) * fixing issue with comparison on some platforms (MIPS), as unsigned arithmetic seems to be the default Co-authored-by: Pat Buah <38233063+EditUndo@users.noreply.github.com> --- lib/pkcs11_mechanism.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/pkcs11_mechanism.c b/lib/pkcs11_mechanism.c index fe40fc87..eb81727b 100644 --- a/lib/pkcs11_mechanism.c +++ b/lib/pkcs11_mechanism.c @@ -56,9 +56,16 @@ static int compare_CKM_type( const void *a, const void *b) { /* because we are making a comparison between unsigned long, int might not reflect well */ /* we need to use an intermediary value and divide it by itself (as absolute value) */ - - long long item = ((MechanismDesc *)a)->type - ((MechanismDesc *)b)->type; + /* we explicitely use "signed" as some platform (MIPS) seem to work with unsigned by default */ + //avoiding undefined behaviour + MechanismDesc* mech_a = (MechanismDesc*)a; + MechanismDesc* mech_b = (MechanismDesc*)b; + if(!mech_a || !mech_b) { + fprintf(stderr, "***Error: failed to detect valid mechanism description...exiting.\n"); + exit(rc_error_invalid_argument); + } + signed long long item = (signed long long)(mech_a->type) - (signed long long)(mech_b->type); return item ? item/llabs(item) : 0; }