diff options
author | Claudio Jeker <claudio@cvs.openbsd.org> | 2024-02-22 13:17:19 +0000 |
---|---|---|
committer | Claudio Jeker <claudio@cvs.openbsd.org> | 2024-02-22 13:17:19 +0000 |
commit | 396bd04c7d33a00022b718a1b444abce8906e826 (patch) | |
tree | 42c0b571906448348ecfd8f2ef79e249c9b94bed | |
parent | 1ee4a27bc972a07e04ef7a23d036ac3d0d0e61cf (diff) |
Rewrite the it_cmp() function to use the common check bigger than, check
smaller than logic.
There was a bug in this code because of a badly placed ) which I only
noticed after rewriting the function since I assumed that C integer
promotion is playing tricks with us.
OK mpi@
-rw-r--r-- | usr.bin/ctfconv/parse.c | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/usr.bin/ctfconv/parse.c b/usr.bin/ctfconv/parse.c index 1b271c8d225..ef483f914ad 100644 --- a/usr.bin/ctfconv/parse.c +++ b/usr.bin/ctfconv/parse.c @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.c,v 1.19 2024/02/21 13:24:37 claudio Exp $ */ +/* $OpenBSD: parse.c,v 1.20 2024/02/22 13:17:18 claudio Exp $ */ /* * Copyright (c) 2016-2017 Martin Pieuchot @@ -323,20 +323,30 @@ it_free(struct itype *it) int it_cmp(struct itype *a, struct itype *b) { - int diff; - - if ((diff = (a->it_type - b->it_type)) != 0) - return diff; - - /* Basic types need to have the same size. */ - if ((a->it_type == CTF_K_INTEGER || a->it_type == CTF_K_FLOAT) && - (diff = (a->it_size - b->it_size) != 0)) - return diff; + if (a->it_type > b->it_type) + return 1; + if (a->it_type < b->it_type) + return -1; + + /* Basic types need to have the same encoding and size. */ + if ((a->it_type == CTF_K_INTEGER || a->it_type == CTF_K_FLOAT)) { + if (a->it_enc > b->it_enc) + return 1; + if (a->it_enc < b->it_enc) + return -1; + if (a->it_size > b->it_size) + return 1; + if (a->it_size < b->it_size) + return -1; + } /* Arrays need to have same number of elements */ - if ((a->it_type == CTF_K_ARRAY) && - (diff = (a->it_nelems - b->it_nelems) != 0)) - return diff; + if (a->it_type == CTF_K_ARRAY) { + if (a->it_nelems > b->it_nelems) + return 1; + if (a->it_nelems < b->it_nelems) + return -1; + } /* Match by name */ if (!(a->it_flags & ITF_ANON) && !(b->it_flags & ITF_ANON)) @@ -349,6 +359,10 @@ it_cmp(struct itype *a, struct itype *b) /* Match by reference */ if ((a->it_refp != NULL) && (b->it_refp != NULL)) return it_cmp(a->it_refp, b->it_refp); + if (a->it_refp == NULL) + return -1; + if (b->it_refp == NULL) + return 1; return 0; } |