Error out if a value is out of range

psa_status_t is currently a signed type where only non-negative values
are used, which makes things a bit awkward. For now, non-negative
values trigger an error. This code will need to be revised if we
switch to using negative values as error codes.
This commit is contained in:
Gilles Peskine 2018-10-31 14:52:28 +01:00 committed by Darryl Green
parent 182c2e9836
commit 265a171c52

View file

@ -160,6 +160,7 @@ typedef enum {
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
value_type type; value_type type;
unsigned long max;
int i; int i;
if (argc <= 1 || if (argc <= 1 ||
@ -172,14 +173,19 @@ int main(int argc, char *argv[])
if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) { if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) {
type = TYPE_STATUS; type = TYPE_STATUS;
max = 0x7fffffff; /* hard-coded because psa_status_t is signed */
} else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) { } else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) {
type = TYPE_ALGORITHM; type = TYPE_ALGORITHM;
max = (psa_algorithm_t)( -1 );
} else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) { } else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) {
type = TYPE_ECC_CURVE; type = TYPE_ECC_CURVE;
max = (psa_ecc_curve_t)( -1 );
} else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) { } else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) {
type = TYPE_KEY_TYPE; type = TYPE_KEY_TYPE;
max = (psa_key_type_t)( -1 );
} else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) { } else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) {
type = TYPE_KEY_USAGE; type = TYPE_KEY_USAGE;
max = (psa_key_usage_t)( -1 );
} else { } else {
printf("Unknown type: %s\n", argv[1]); printf("Unknown type: %s\n", argv[1]);
return EXIT_FAILURE; return EXIT_FAILURE;
@ -193,6 +199,10 @@ int main(int argc, char *argv[])
printf("Non-numeric value: %s\n", argv[i]); printf("Non-numeric value: %s\n", argv[i]);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (value > max) {
printf("Value out of range: %s\n", argv[i]);
return EXIT_FAILURE;
}
switch (type) { switch (type) {
case TYPE_STATUS: case TYPE_STATUS: