spa: fix integer overflows etc. in spa_pod_compare_value

Add macro SPA_CMP to do 3-way comparisons safely, and use it to avoid
signed integer overflows.

Fix also float/double comparisons (previously 0.1 == 0.8 since cast to
return type int).

Fix Id/Bool comparisons so they can return negative value.
This commit is contained in:
Pauli Virtanen 2024-05-18 15:46:32 +03:00 committed by Wim Taymans
parent b94d6e53a1
commit a63aa6329b
3 changed files with 28 additions and 14 deletions

View file

@ -186,6 +186,16 @@ struct spa_fraction {
x; \
})
/** 3-way comparison. NaN > NaN and NaN > finite numbers */
#define SPA_CMP(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
(_a > _b) ? 1 : (_a == _b) ? 0 : (_a < _b) ? -1 \
: (_a == _a) ? -1 : (_b == _b) ? 1 \
: 1; \
})
/**
* Return the address (buffer + offset) as pointer of \a type
*/