Skip to content

Commit 028d9b4

Browse files
committed
make ubsan happy by removing duplication
1 parent 2d9ae2f commit 028d9b4

File tree

2 files changed

+15
-72
lines changed

2 files changed

+15
-72
lines changed

src/protocols/der/decode.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ static ssize_t fr_der_decode_octetstring(TALLOC_CTX *ctx, fr_pair_list_t *out, f
9797
static ssize_t fr_der_decode_null(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent, fr_dbuff_t *in,
9898
fr_der_decode_ctx_t *decode_ctx) CC_HINT(nonnull);
9999

100-
static ssize_t fr_der_decode_enumerated(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent,
101-
fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx) CC_HINT(nonnull);
102-
103100
static ssize_t fr_der_decode_utf8_string(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent,
104101
fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx) CC_HINT(nonnull);
105102

@@ -133,6 +130,13 @@ static ssize_t fr_der_decode_general_string(TALLOC_CTX *ctx, fr_pair_list_t *out
133130
static ssize_t fr_der_decode_universal_string(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent,
134131
fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx) CC_HINT(nonnull);
135132

133+
/*
134+
* We have per-type function names to make it clear that different types have different decoders.
135+
* However, the methods to decode them are the same. So rather than having trampoline functions, we just
136+
* use defines.
137+
*/
138+
#define fr_der_decode_enumerated fr_der_decode_integer
139+
136140
static fr_der_tag_decode_t tag_funcs[] = {
137141
[FR_DER_TAG_BOOLEAN] = { .constructed = FR_DER_TAG_PRIMITIVE, .decode = fr_der_decode_boolean },
138142
[FR_DER_TAG_INTEGER] = { .constructed = FR_DER_TAG_PRIMITIVE, .decode = fr_der_decode_integer },
@@ -829,12 +833,6 @@ static ssize_t fr_der_decode_oid(UNUSED fr_pair_list_t *out, fr_dbuff_t *in, fr_
829833
return fr_dbuff_set(in, &our_in);
830834
}
831835

832-
static ssize_t fr_der_decode_enumerated(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent,
833-
fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
834-
{
835-
return fr_der_decode_integer(ctx, out, parent, in, decode_ctx);
836-
}
837-
838836
static ssize_t fr_der_decode_utf8_string(TALLOC_CTX *ctx, fr_pair_list_t *out, fr_dict_attr_t const *parent,
839837
fr_dbuff_t *in, fr_der_decode_ctx_t *decode_ctx)
840838
{

src/protocols/der/encode.c

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ static ssize_t fr_der_encode_bitstring(fr_dbuff_t *dbuff, fr_dcursor_t *cursor,
7070
static ssize_t fr_der_encode_octetstring(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx) CC_HINT(nonnull);
7171
static ssize_t fr_der_encode_null(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx) CC_HINT(nonnull(2));
7272
static ssize_t fr_der_encode_oid(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx) CC_HINT(nonnull(1,2));
73-
static ssize_t fr_der_encode_enumerated(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx) CC_HINT(nonnull(1,2));
7473
static ssize_t fr_der_encode_sequence(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx) CC_HINT(nonnull);
7574
static ssize_t fr_der_encode_set(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx) CC_HINT(nonnull);
7675
static ssize_t fr_der_encode_utc_time(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx) CC_HINT(nonnull(1,2));
@@ -80,6 +79,14 @@ static ssize_t fr_der_encode_oid_value_pair(fr_dbuff_t *dbuff, fr_dcursor_t *cur
8079

8180
static ssize_t fr_der_encode_string(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx) CC_HINT(nonnull(1,2));
8281

82+
/*
83+
* We have per-type function names to make it clear that different types have different encoders.
84+
* However, the methods to encode them are the same. So rather than having trampoline functions, we just
85+
* use defines.
86+
*/
87+
#define fr_der_encode_enumerated fr_der_encode_integer
88+
89+
8390
static ssize_t fr_der_encode_len(fr_dbuff_t *dbuff, fr_dbuff_marker_t *length_start, ssize_t len) CC_HINT(nonnull);
8491
static inline CC_HINT(always_inline) ssize_t
8592
fr_der_encode_tag(fr_dbuff_t *dbuff, fr_der_tag_num_t tag_num, fr_der_tag_class_t tag_class,
@@ -573,68 +580,6 @@ static ssize_t fr_der_encode_oid(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED
573580
return fr_dbuff_set(dbuff, &our_dbuff);
574581
}
575582

576-
static ssize_t fr_der_encode_enumerated(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
577-
{
578-
fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
579-
fr_pair_t const *vp;
580-
int64_t value;
581-
uint8_t first_octet = 0;
582-
size_t i, len;
583-
584-
vp = fr_dcursor_current(cursor);
585-
PAIR_VERIFY(vp);
586-
587-
/*
588-
* ISO/IEC 8825-1:2021
589-
* 8.4 Encoding of an enumerated value
590-
* The encoding of an enumerated value shall be that of the integer value with which it is
591-
* associated.
592-
* NOTE - It is primitive.
593-
*/
594-
value = vp->vp_int64;
595-
596-
for (i = 0, len = 0; i < sizeof(value); i++) {
597-
uint8_t byte = (value >> 56) & 0xff;
598-
599-
value <<= 8;
600-
601-
if (len == 0) {
602-
first_octet = byte;
603-
len++;
604-
continue;
605-
606-
} else if (len == 1) {
607-
/*
608-
* 8.3.2 If the contents octets of an integer value encoding consist of more than one
609-
* octet, then the bits of the first octet and bit 8 of the second octet: a) shall not
610-
* all be ones; and b) shall not all be zero.
611-
*/
612-
if ((first_octet == 0xff && (byte & 0x80)) || ((first_octet == 0x00) && (byte >> 7 == 0))) {
613-
if (i == sizeof(value) - 1) {
614-
/*
615-
* If this is the only byte, then we can encode it in a single byte.
616-
*/
617-
FR_DBUFF_IN_RETURN(&our_dbuff, byte);
618-
continue;
619-
}
620-
621-
first_octet = byte;
622-
continue;
623-
} else {
624-
FR_DBUFF_IN_RETURN(&our_dbuff, first_octet);
625-
FR_DBUFF_IN_RETURN(&our_dbuff, byte);
626-
len++;
627-
continue;
628-
}
629-
}
630-
631-
FR_DBUFF_IN_RETURN(&our_dbuff, byte);
632-
len++;
633-
}
634-
635-
return fr_dbuff_set(dbuff, &our_dbuff);
636-
}
637-
638583
static ssize_t fr_der_encode_sequence(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, fr_der_encode_ctx_t *encode_ctx)
639584
{
640585
fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);

0 commit comments

Comments
 (0)