From 1824696681de10a1f01048ef7e3bdd21f407a788 Mon Sep 17 00:00:00 2001 From: Darryl Green Date: Wed, 17 Oct 2018 15:01:45 +0100 Subject: [PATCH 1/4] Fix integer conversion warnings in psa_constant_names --- programs/psa/psa_constant_names.c | 18 +++++++++--------- scripts/generate_psa_constants.py | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/programs/psa/psa_constant_names.c b/programs/psa/psa_constant_names.c index 55a70c60d..88821dab5 100644 --- a/programs/psa/psa_constant_names.c +++ b/programs/psa/psa_constant_names.c @@ -83,9 +83,9 @@ static int psa_snprint_status(char *buffer, size_t buffer_size, size_t length = strlen(name); if (length < buffer_size) { memcpy(buffer, name, length + 1); - return length; + return (int) length; } else { - return buffer_size; + return (int) buffer_size; } } } @@ -100,9 +100,9 @@ static int psa_snprint_ecc_curve(char *buffer, size_t buffer_size, size_t length = strlen(name); if (length < buffer_size) { memcpy(buffer, name, length + 1); - return length; + return (int) length; } else { - return buffer_size; + return (int) buffer_size; } } } @@ -144,15 +144,15 @@ int main(int argc, char *argv[]) } if (!strcmp(argv[1], "error") || !strcmp(argv[1], "status")) - psa_snprint_status(buffer, sizeof(buffer), value); + psa_snprint_status(buffer, sizeof(buffer), (psa_status_t) value); else if (!strcmp(argv[1], "alg") || !strcmp(argv[1], "algorithm")) - psa_snprint_algorithm(buffer, sizeof(buffer), value); + psa_snprint_algorithm(buffer, sizeof(buffer), (psa_algorithm_t) value); else if (!strcmp(argv[1], "curve") || !strcmp(argv[1], "ecc_curve")) - psa_snprint_ecc_curve(buffer, sizeof(buffer), value); + psa_snprint_ecc_curve(buffer, sizeof(buffer), (psa_ecc_curve_t) value); else if (!strcmp(argv[1], "type") || !strcmp(argv[1], "key_type")) - psa_snprint_key_type(buffer, sizeof(buffer), value); + psa_snprint_key_type(buffer, sizeof(buffer), (psa_key_type_t) value); else if (!strcmp(argv[1], "usage") || !strcmp(argv[1], "key_usage")) - psa_snprint_key_usage(buffer, sizeof(buffer), value); + psa_snprint_key_usage(buffer, sizeof(buffer), (psa_key_usage_t) value); else { printf("Unknown type: %s\n", argv[1]); return EXIT_FAILURE; diff --git a/scripts/generate_psa_constants.py b/scripts/generate_psa_constants.py index 85bfe3ae9..7e4420b69 100755 --- a/scripts/generate_psa_constants.py +++ b/scripts/generate_psa_constants.py @@ -44,7 +44,7 @@ static int psa_snprint_key_type(char *buffer, size_t buffer_size, break; } buffer[0] = 0; - return required_size; + return (int) required_size; } static int psa_snprint_algorithm(char *buffer, size_t buffer_size, @@ -84,7 +84,7 @@ static int psa_snprint_algorithm(char *buffer, size_t buffer_size, append(&buffer, buffer_size, &required_size, ")", 1); } buffer[0] = 0; - return required_size; + return (int) required_size; } static int psa_snprint_key_usage(char *buffer, size_t buffer_size, @@ -110,7 +110,7 @@ static int psa_snprint_key_usage(char *buffer, size_t buffer_size, } else { buffer[0] = 0; } - return required_size; + return (int) required_size; } /* End of automatically generated file. */ From 608e091d9a44f562d8b240d9602cbb268eab1b6d Mon Sep 17 00:00:00 2001 From: Darryl Green Date: Wed, 17 Oct 2018 14:48:27 +0100 Subject: [PATCH 2/4] Add pre Visual Studio 2015 support to psa_constant_names snprintf was only added in Visual Studio 2015. This adds support for building using Visual Studio versions prior to 2015. This implementation of snprintf has been taken from platform.c --- programs/psa/psa_constant_names.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/programs/psa/psa_constant_names.c b/programs/psa/psa_constant_names.c index 88821dab5..dd19677c4 100644 --- a/programs/psa/psa_constant_names.c +++ b/programs/psa/psa_constant_names.c @@ -4,6 +4,35 @@ #include "psa/crypto.h" +/* This block is present to support Visual Studio builds prior to 2015 */ +#if defined(_MSC_VER) && _MSC_VER < 1900 +#include +int snprintf( char *s, size_t n, const char *fmt, ... ) +{ + int ret; + va_list argp; + + /* Avoid calling the invalid parameter handler by checking ourselves */ + if( s == NULL || n == 0 || fmt == NULL ) + return( -1 ); + + va_start( argp, fmt ); +#if defined(_TRUNCATE) && !defined(__MINGW32__) + ret = _vsnprintf_s( s, n, _TRUNCATE, fmt, argp ); +#else + ret = _vsnprintf( s, n, fmt, argp ); + if( ret < 0 || (size_t) ret == n ) + { + s[n-1] = '\0'; + ret = -1; + } +#endif + va_end( argp ); + + return( ret ); +} +#endif + /* There are different GET_HASH macros for different kinds of algorithms * built from hashes, but the values are all constructed on the * same model. */ From 3b80ab93ce0c6e173c773622f77b2899c949ef0b Mon Sep 17 00:00:00 2001 From: Darryl Green Date: Wed, 17 Oct 2018 16:11:34 +0100 Subject: [PATCH 3/4] Add path handling for psa_constant_names on Windows --- programs/Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/programs/Makefile b/programs/Makefile index b1534071c..9cc28c47e 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -96,7 +96,12 @@ all: $(APPS) $(DEP): $(MAKE) -C ../library +ifdef WINDOWS +EXTRA_GENERATED += psa\psa_constant_names_generated.c +else EXTRA_GENERATED += psa/psa_constant_names_generated.c +endif + psa/psa_constant_names$(EXEXT): psa/psa_constant_names_generated.c psa/psa_constant_names_generated.c: ../scripts/generate_psa_constants.py ../include/psa/crypto.h ../scripts/generate_psa_constants.py From 6c0f94cbd0ede1dad6383ea27a8d8569123afefc Mon Sep 17 00:00:00 2001 From: Darryl Green Date: Wed, 17 Oct 2018 16:12:33 +0100 Subject: [PATCH 4/4] Add better handling when deleting files on Windows Windows complains if you try to delete a file that doesn't exist. Makefiles now check if the files exist before trying to delete them. --- library/Makefile | 3 ++- programs/Makefile | 4 +++- tests/Makefile | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/library/Makefile b/library/Makefile index f4b39bdeb..cf6750d05 100644 --- a/library/Makefile +++ b/library/Makefile @@ -200,5 +200,6 @@ clean: ifndef WINDOWS rm -f *.o libmbed* else - del /Q /F *.o libmbed* + if exist *.o del /Q /F *.o + if exist libmbed* del /Q /F libmbed* endif diff --git a/programs/Makefile b/programs/Makefile index 9cc28c47e..f3627c906 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -322,7 +322,9 @@ clean: ifndef WINDOWS rm -f $(APPS) $(EXTRA_GENERATED) else - del /S /Q /F *.o *.exe $(EXTRA_GENERATED) + if exist *.o del /S /Q /F *.o + if exist *.exe del /S /Q /F *.exe + if exist $(EXTRA_GENERATED) del /S /Q /F $(EXTRA_GENERATED) endif list: diff --git a/tests/Makefile b/tests/Makefile index b6e49bf8a..889d2a7da 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -107,7 +107,9 @@ clean: ifndef WINDOWS rm -rf $(BINARIES) *.c *.datax TESTS else - del /Q /F *.c *.exe *.datax + if exist *.c del /Q /F *.c + if exist *.exe del /Q /F *.exe + if exist *.datax del /Q /F *.datax ifneq ($(wildcard TESTS/.*),) rmdir /Q /S TESTS endif