asn1: Mitigate const abuse and omit needless casts.

There's still an abusive rk_UNCONST in _asn1_encode_open_type, but
that will take more effort to untangle.
This commit is contained in:
Taylor R Campbell
2023-06-20 01:00:33 +00:00
committed by Nicolas Williams
parent f494333624
commit 9765e2522b
6 changed files with 81 additions and 72 deletions

View File

@@ -185,7 +185,7 @@ extern int rfc1510_bitstring;
extern int one_code_file;
extern int original_order;
extern int parse_units_flag;
extern char *type_file_string;
extern const char *type_file_string;
extern int error_flag;

View File

@@ -938,7 +938,7 @@ template_members(struct templatehead *temp,
case TEnumerated:
case TInteger: {
char *varname = NULL;
char *itype = NULL;
const char *itype = NULL;
if (t->members)
itype = "IMEMBER";

View File

@@ -44,9 +44,9 @@ static getarg_strings decorate;
static int
strcmp4mergesort_r(const void *ap, const void *bp, void *d)
{
const char *a = *(const char **)ap;
const char *b = *(const char **)bp;
char sep = *(const char *)d;
const char *a = *(const char *const *)ap;
const char *b = *(const char *const *)bp;
char sep = **(const char **)d;
int cmp;
if (sep) {
@@ -260,7 +260,7 @@ int rfc1510_bitstring;
int one_code_file;
char *option_file;
int parse_units_flag = 1;
char *type_file_string = "krb5-types.h";
const char *type_file_string = "krb5-types.h";
int original_order;
int version_flag;
int help_flag;
@@ -457,15 +457,21 @@ main(int argc, char **argv)
#endif
}
if (preserve.num_strings)
if (preserve.num_strings) {
const char *sep = "";
mergesort_r(preserve.strings, preserve.num_strings,
sizeof(preserve.strings[0]), strcmp4mergesort_r, "");
if (seq.num_strings)
sizeof(preserve.strings[0]), strcmp4mergesort_r, &sep);
}
if (seq.num_strings) {
const char *sep = "";
mergesort_r(seq.strings, seq.num_strings, sizeof(seq.strings[0]),
strcmp4mergesort_r, "");
if (decorate.num_strings)
strcmp4mergesort_r, &sep);
}
if (decorate.num_strings) {
const char *sep = ":";
mergesort_r(decorate.strings, decorate.num_strings,
sizeof(decorate.strings[0]), strcmp4mergesort_r, ":");
sizeof(decorate.strings[0]), strcmp4mergesort_r, &sep);
}
init_generate(file, name);

View File

@@ -74,7 +74,7 @@ output_name(char *s)
}
Symbol *
addsym(char *name)
addsym(const char *name)
{
Symbol key, *s;
@@ -101,7 +101,7 @@ addsym(char *name)
}
Symbol *
getsym(char *name)
getsym(const char *name)
{
Symbol key;

View File

@@ -230,7 +230,7 @@ struct objid {
};
struct symbol {
char *name;
const char *name;
char *gen_name;
enum { SUndefined, SValue, Stype, Sparamtype, Sclass, Sobj, Sobjset } stype;
struct value *value;
@@ -257,8 +257,8 @@ struct symhead {
extern struct symhead symbols;
void initsym (void);
Symbol *addsym (char *);
Symbol *getsym(char *name);
Symbol *addsym (const char *);
Symbol *getsym(const char *);
void output_name (char *);
int checkundefined(void);
void generate_types(void);

View File

@@ -131,11 +131,11 @@ int
_asn1_bmember_isset_bit(const void *data, unsigned int bit, size_t size)
{
#ifdef WORDS_BIGENDIAN
if ((*(unsigned int *)data) & (1u << ((size * 8) - bit - 1)))
if ((*(const unsigned int *)data) & (1u << ((size * 8) - bit - 1)))
return 1;
return 0;
#else
if ((*(unsigned int *)data) & (1u << bit))
if ((*(const unsigned int *)data) & (1u << bit))
return 1;
return 0;
#endif
@@ -817,7 +817,7 @@ _asn1_decode(const struct asn1_template *t, unsigned flags,
case A1_OP_TYPE_EXTERN: {
size_t newsize, elsize;
void *el = DPO(data, t->offset);
void **pel = (void **)el;
void **pel = el;
if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
elsize = _asn1_sizeofType(t->ptr);
@@ -870,19 +870,19 @@ _asn1_decode(const struct asn1_template *t, unsigned flags,
* though we should really look more carefully at `ret'.
*/
if (tdefval->tt & A1_DV_BOOLEAN) {
int *i = (void *)(char *)el;
int *i = el;
*i = tdefval->ptr ? 1 : 0;
} else if (tdefval->tt & A1_DV_INTEGER64) {
int64_t *i = (void *)(char *)el;
int64_t *i = el;
*i = (int64_t)(intptr_t)tdefval->ptr;
} else if (tdefval->tt & A1_DV_INTEGER32) {
int32_t *i = (void *)(char *)el;
int32_t *i = el;
*i = (int32_t)(intptr_t)tdefval->ptr;
} else if (tdefval->tt & A1_DV_INTEGER) {
struct heim_integer *i = (void *)(char *)el;
struct heim_integer *i = el;
if ((ret = der_copy_heim_integer(tdefval->ptr, i)))
return ret;
@@ -911,7 +911,7 @@ _asn1_decode(const struct asn1_template *t, unsigned flags,
int subflags = flags;
int replace_tag = (t->tt & A1_FLAG_IMPLICIT) && is_tagged(t->ptr);
void *el = data = DPO(data, t->offset);
void **pel = (void **)el;
void **pel = el;
/*
* XXX If this type (chasing t->ptr through IMPLICIT tags, if this
@@ -938,19 +938,19 @@ _asn1_decode(const struct asn1_template *t, unsigned flags,
* though we should really look more carefully at `ret'.
*/
if (tdefval->tt & A1_DV_BOOLEAN) {
int *i = (void *)(char *)data;
int *i = data;
*i = tdefval->ptr ? 1 : 0;
} else if (tdefval->tt & A1_DV_INTEGER64) {
int64_t *i = (void *)(char *)data;
int64_t *i = data;
*i = (int64_t)(intptr_t)tdefval->ptr;
} else if (tdefval->tt & A1_DV_INTEGER32) {
int32_t *i = (void *)(char *)data;
int32_t *i = data;
*i = (int32_t)(intptr_t)tdefval->ptr;
} else if (tdefval->tt & A1_DV_INTEGER) {
struct heim_integer *i = (void *)(char *)data;
struct heim_integer *i = data;
if ((ret = der_copy_heim_integer(tdefval->ptr, i)))
return ret;
@@ -1288,10 +1288,11 @@ typeid_int_copy(void *intp,
/* See commentary in _asn1_decode_open_type() */
static int
_asn1_encode_open_type(const struct asn1_template *t,
const void *data, /* NOTE: Not really const */
const void *cdata, /* NOTE: Not really const */
const struct asn1_template *ttypeid,
const struct asn1_template *topentype)
{
void *data = rk_UNCONST(cdata); /* XXX */
const struct asn1_template *ttypeid_univ = ttypeid;
const struct asn1_template *tactual_type;
const struct asn1_template *tos = t->ptr;
@@ -1356,12 +1357,13 @@ _asn1_encode_open_type(const struct asn1_template *t,
* values.
*/
if (!(t->tt & A1_OS_OT_IS_ARRAY)) {
struct heim_base_data *os = DPO(data, topentype->offset);
const struct heim_base_data *os = DPOC(data, topentype->offset);
if (os->length && os->data)
return 0;
} else {
struct heim_base_data **os = DPO(data, topentype->offset + sizeof(len));
struct heim_base_data **os =
DPO(data, topentype->offset + sizeof(len));
while (sizeof(void *) != sizeof(unsigned int) &&
((uintptr_t)os) % sizeof(void *) != 0)
@@ -1403,7 +1405,7 @@ _asn1_encode_open_type(const struct asn1_template *t,
while (sizeof(void *) != sizeof(element) &&
((uintptr_t)d) % sizeof(void *) != 0) {
d = (void *)(((char *)d) + sizeof(element));
d = (const void *)(((const char *)d) + sizeof(element));
}
os->length = _asn1_length(tactual_type->ptr, *d);
@@ -1471,7 +1473,7 @@ _asn1_encode(const struct asn1_template *t, unsigned char *p, size_t len, const
const void *el = DPOC(data, t->offset);
if (t->tt & A1_FLAG_OPTIONAL) {
void **pel = (void **)el;
const void *const *pel = el;
if (*pel == NULL)
break;
el = *pel;
@@ -1479,24 +1481,24 @@ _asn1_encode(const struct asn1_template *t, unsigned char *p, size_t len, const
const struct asn1_template *tdefval = t - 1;
/* Compare tdefval to whatever's at `el' */
if (tdefval->tt & A1_DV_BOOLEAN) {
const int *i = (void *)(char *)el;
const int *i = el;
if ((*i && tdefval->ptr) || (!*i && !tdefval->ptr))
break;
} else if (tdefval->tt & A1_DV_INTEGER64) {
const int64_t *i = (void *)(char *)el;
const int64_t *i = el;
if (*i == (int64_t)(intptr_t)tdefval->ptr)
break;
} else if (tdefval->tt & A1_DV_INTEGER32) {
const int32_t *i = (void *)(char *)el;
const int32_t *i = el;
if ((int64_t)(intptr_t)tdefval->ptr <= INT_MAX &&
(int64_t)(intptr_t)tdefval->ptr >= INT_MIN &&
*i == (int32_t)(intptr_t)tdefval->ptr)
break;
} else if (tdefval->tt & A1_DV_INTEGER) {
const struct heim_integer *i = (void *)(char *)el;
const struct heim_integer *i = el;
if (der_heim_integer_cmp(i, tdefval->ptr) == 0)
break;
@@ -1541,7 +1543,7 @@ _asn1_encode(const struct asn1_template *t, unsigned char *p, size_t len, const
data = DPOC(data, t->offset);
if (t->tt & A1_FLAG_OPTIONAL) {
void **el = (void **)data;
const void *const *el = data;
if (*el == NULL) {
data = olddata;
break;
@@ -1553,24 +1555,24 @@ _asn1_encode(const struct asn1_template *t, unsigned char *p, size_t len, const
/* Compare tdefval to whatever's at `data' */
if (tdefval->tt & A1_DV_BOOLEAN) {
const int *i = (void *)(char *)data;
const int *i = data;
if ((*i && tdefval->ptr) || (!*i && !tdefval->ptr))
exclude = 1;
} else if (tdefval->tt & A1_DV_INTEGER64) {
const int64_t *i = (void *)(char *)data;
const int64_t *i = data;
if (*i == (int64_t)(intptr_t)tdefval->ptr)
exclude = 1;
} else if (tdefval->tt & A1_DV_INTEGER32) {
const int32_t *i = (void *)(char *)data;
const int32_t *i = data;
if ((int64_t)(intptr_t)tdefval->ptr <= INT_MAX &&
(int64_t)(intptr_t)tdefval->ptr >= INT_MIN &&
*i == (int32_t)(intptr_t)tdefval->ptr)
exclude = 1;
} else if (tdefval->tt & A1_DV_INTEGER) {
const struct heim_integer *i = (void *)(char *)data;
const struct heim_integer *i = data;
if (der_heim_integer_cmp(i, tdefval->ptr) == 0)
break;
@@ -1744,7 +1746,7 @@ _asn1_encode(const struct asn1_template *t, unsigned char *p, size_t len, const
}
case A1_OP_SEQOF: {
struct template_of *el = DPO(data, t->offset);
const struct template_of *el = DPOC(data, t->offset);
size_t ellen = _asn1_sizeofType(t->ptr);
size_t newsize;
unsigned int i;
@@ -1945,16 +1947,17 @@ _asn1_length_open_type(const struct asn1_template *tbase,
default: return 0;
}
if (!(t->tt & A1_OS_OT_IS_ARRAY)) {
struct heim_base_data *os = DPO(data, topentype->offset);
const struct heim_base_data *os = DPOC(data, topentype->offset);
if (os->length && os->data)
return 0;
} else {
struct heim_base_data **os = DPO(data, topentype->offset + sizeof(len));
const struct heim_base_data *const *os =
DPOC(data, topentype->offset + sizeof(len));
while (sizeof(void *) != sizeof(unsigned int) &&
((uintptr_t)os) % sizeof(void *) != 0)
os = (void *)(((char *)os) + sizeof(unsigned int));
os = (const void *)(((const char *)os) + sizeof(unsigned int));
lenp = DPOC(data, topentype->offset);
if (*lenp == len && os[0]->length && os[0]->data)
@@ -1997,7 +2000,7 @@ _asn1_length_open_type(const struct asn1_template *tbase,
while (sizeof(void *) != sizeof(element) &&
((uintptr_t)d) % sizeof(void *) != 0)
d = (void *)(((char *)d) + sizeof(element));
d = (const void *)(((const char *)d) + sizeof(element));
if (*d)
sz += _asn1_length(tactual_type->ptr, *d);
} else {
@@ -2048,7 +2051,7 @@ _asn1_length(const struct asn1_template *t, const void *data)
const void *el = DPOC(data, t->offset);
if (t->tt & A1_FLAG_OPTIONAL) {
void **pel = (void **)el;
void *const *pel = el;
if (*pel == NULL)
break;
el = *pel;
@@ -2057,24 +2060,24 @@ _asn1_length(const struct asn1_template *t, const void *data)
/* Compare tdefval to whatever's at `el' */
if (tdefval->tt & A1_DV_BOOLEAN) {
const int *i = (void *)(char *)el;
const int *i = el;
if ((*i && tdefval->ptr) || (!*i && !tdefval->ptr))
break;
} else if (tdefval->tt & A1_DV_INTEGER64) {
const int64_t *i = (void *)(char *)el;
const int64_t *i = el;
if (*i == (int64_t)(intptr_t)tdefval->ptr)
break;
} else if (tdefval->tt & A1_DV_INTEGER32) {
const int32_t *i = (void *)(char *)el;
const int32_t *i = el;
if ((int64_t)(intptr_t)tdefval->ptr <= INT_MAX &&
(int64_t)(intptr_t)tdefval->ptr >= INT_MIN &&
*i == (int32_t)(intptr_t)tdefval->ptr)
break;
} else if (tdefval->tt & A1_DV_INTEGER) {
const struct heim_integer *i = (void *)(char *)el;
const struct heim_integer *i = el;
if (der_heim_integer_cmp(i, tdefval->ptr) == 0)
break;
@@ -2101,10 +2104,10 @@ _asn1_length(const struct asn1_template *t, const void *data)
const void *olddata = data;
size_t oldtaglen = 0;
data = DPO(data, t->offset);
data = DPOC(data, t->offset);
if (t->tt & A1_FLAG_OPTIONAL) {
void **el = (void **)data;
void *const *el = data;
if (*el == NULL) {
data = olddata;
break;
@@ -2116,24 +2119,24 @@ _asn1_length(const struct asn1_template *t, const void *data)
/* Compare tdefval to whatever's at `data' */
if (tdefval->tt & A1_DV_BOOLEAN) {
const int *i = (void *)(char *)data;
const int *i = data;
if ((*i && tdefval->ptr) || (!*i && !tdefval->ptr))
exclude = 1;
} else if (tdefval->tt & A1_DV_INTEGER64) {
const int64_t *i = (void *)(char *)data;
const int64_t *i = data;
if (*i == (int64_t)(intptr_t)tdefval->ptr)
exclude = 1;
} else if (tdefval->tt & A1_DV_INTEGER32) {
const int32_t *i = (void *)(char *)data;
const int32_t *i = data;
if ((int64_t)(intptr_t)tdefval->ptr <= INT_MAX &&
(int64_t)(intptr_t)tdefval->ptr >= INT_MIN &&
*i == (int32_t)(intptr_t)tdefval->ptr)
exclude = 1;
} else if (tdefval->tt & A1_DV_INTEGER) {
const struct heim_integer *i = (void *)(char *)data;
const struct heim_integer *i = data;
if (der_heim_integer_cmp(i, tdefval->ptr) == 0)
exclude = 1;
@@ -2310,7 +2313,7 @@ _asn1_free(const struct asn1_template *t, void *data)
case A1_OP_TYPE:
case A1_OP_TYPE_EXTERN: {
void *el = DPO(data, t->offset);
void **pel = (void **)el;
void **pel = el;
if (t->tt & A1_FLAG_OPTIONAL) {
if (*pel == NULL)
@@ -2354,7 +2357,7 @@ _asn1_free(const struct asn1_template *t, void *data)
void *el = DPO(data, t->offset);
if (t->tt & A1_FLAG_OPTIONAL) {
void **pel = (void **)el;
void **pel = el;
if (*pel == NULL)
break;
@@ -2508,7 +2511,7 @@ _asn1_print_open_type(const struct asn1_template *t, /* object set template */
dp = DPOC(data, t->offset + sizeof(*elementp) + sizeof(*lenp));
while (sizeof(void *) != sizeof(*elementp) &&
((uintptr_t)dp) % sizeof(void *) != 0)
dp = (void *)(((char *)dp) + sizeof(*elementp));
dp = (const void *)(((const char *)dp) + sizeof(*elementp));
val = *dp;
r = rk_strpoolprintf(r, ",%s\"_%s\":[", indents ? indents : "",
@@ -2659,7 +2662,7 @@ _asn1_print(const struct asn1_template *t,
size_t left = 0;
size_t right = A1_HEADER_LEN(tenum);
size_t mid;
uint32_t v = *(unsigned int *)el;
uint32_t v = *(const unsigned int *)el;
int c = -1;
while (left <= right) {
@@ -2835,7 +2838,7 @@ _asn1_copy_open_type(const struct asn1_template *t, /* object set template */
void **valto;
unsigned int *lentop;
unsigned int len;
const int *efromp = DPO(from, t->offset);
const int *efromp = DPOC(from, t->offset);
int *etop = DPO(to, t->offset);
int ret = 0;
@@ -2850,10 +2853,10 @@ _asn1_copy_open_type(const struct asn1_template *t, /* object set template */
tactual_type = &tos[3*(*efromp - 1) + 4];
if (!(t->tt & A1_OS_OT_IS_ARRAY)) {
dfromp = DPO(from, t->offset + sizeof(*efromp));
dfromp = DPOC(from, t->offset + sizeof(*efromp));
while (sizeof(void *) != sizeof(*efromp) &&
((uintptr_t)dfromp) % sizeof(void *) != 0)
dfromp = (void *)(((char *)dfromp) + sizeof(*efromp));
dfromp = (const void *)(((const char *)dfromp) + sizeof(*efromp));
if (!*dfromp)
return 0;
@@ -2871,8 +2874,8 @@ _asn1_copy_open_type(const struct asn1_template *t, /* object set template */
return ret;
}
lenfromp = DPO(from, t->offset + sizeof(*efromp));
dfromp = DPO(from, t->offset + sizeof(*efromp) + sizeof(*lenfromp));
lenfromp = DPOC(from, t->offset + sizeof(*efromp));
dfromp = DPOC(from, t->offset + sizeof(*efromp) + sizeof(*lenfromp));
valfrom = *dfromp;
lentop = DPO(to, t->offset + sizeof(*etop));
dtop = DPO(to, t->offset + sizeof(*etop) + sizeof(*lentop));
@@ -2939,7 +2942,7 @@ _asn1_copy(const struct asn1_template *t, const void *from, void *to)
case A1_OP_TYPE_EXTERN: {
const void *fel = DPOC(from, t->offset);
void *tel = DPO(to, t->offset);
void **ptel = (void **)tel;
void **ptel = tel;
size_t size;
if ((t->tt & A1_OP_MASK) == A1_OP_TYPE ||
@@ -2951,7 +2954,7 @@ _asn1_copy(const struct asn1_template *t, const void *from, void *to)
}
if (t->tt & A1_FLAG_OPTIONAL) {
void **pfel = (void **)fel;
const void *const *pfel = fel;
if (*pfel == NULL)
break;
fel = *pfel;
@@ -3009,8 +3012,8 @@ _asn1_copy(const struct asn1_template *t, const void *from, void *to)
to = DPO(to, t->offset);
if (t->tt & A1_FLAG_OPTIONAL) {
void **fel = (void **)from;
tel = (void **)to;
const void *const *fel = from;
tel = to;
if (*fel == NULL) {
from = oldfrom;
to = oldto;