Skip to content

Commit 6ff9c89

Browse files
Merge pull request #10361 from bensze01/runtime-version-interface
Simplify runtime version info string methods
2 parents 89e0c53 + 0e5fe87 commit 6ff9c89

File tree

10 files changed

+29
-43
lines changed

10 files changed

+29
-43
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
API changes
2+
* Change the signature of the runtime version information methods that took
3+
a char* as an argument to take zero arguments and return a const char*
4+
instead. This aligns us with the interface used in TF PSA Crypto 1.0.
5+
If you need to support linking against both Mbed TLS 3.x and 4.x, please
6+
use the build-time version macros or mbedtls_version_get_number() to
7+
determine the correct signature for mbedtls_version_get_string() and
8+
mbedtls_version_get_string_full() before calling them.
9+
Fixes issue #10308.

include/mbedtls/version.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,14 @@ extern "C" {
3232
unsigned int mbedtls_version_get_number(void);
3333

3434
/**
35-
* Get the version string ("x.y.z").
36-
*
37-
* \param string The string that will receive the value.
38-
* (Should be at least 9 bytes in size)
35+
* Get a pointer to the version string ("x.y.z").
3936
*/
40-
void mbedtls_version_get_string(char *string);
37+
const char *mbedtls_version_get_string(void);
4138

4239
/**
43-
* Get the full version string ("Mbed TLS x.y.z").
44-
*
45-
* \param string The string that will receive the value. The Mbed TLS version
46-
* string will use 18 bytes AT MOST including a terminating
47-
* null byte.
48-
* (So the buffer should be at least 18 bytes to receive this
49-
* version string).
40+
* Get a pointer to the full version string ("Mbed TLS x.y.z").
5041
*/
51-
void mbedtls_version_get_string_full(char *string);
42+
const char *mbedtls_version_get_string_full(void);
5243

5344
/**
5445
* \brief Check if support for a feature was compiled into this

library/version.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@ unsigned int mbedtls_version_get_number(void)
1717
return MBEDTLS_VERSION_NUMBER;
1818
}
1919

20-
void mbedtls_version_get_string(char *string)
20+
const char *mbedtls_version_get_string(void)
2121
{
22-
memcpy(string, MBEDTLS_VERSION_STRING,
23-
sizeof(MBEDTLS_VERSION_STRING));
22+
return MBEDTLS_VERSION_STRING;
2423
}
2524

26-
void mbedtls_version_get_string_full(char *string)
25+
const char *mbedtls_version_get_string_full(void)
2726
{
28-
memcpy(string, MBEDTLS_VERSION_STRING_FULL,
29-
sizeof(MBEDTLS_VERSION_STRING_FULL));
27+
return MBEDTLS_VERSION_STRING_FULL;
3028
}
3129

3230
#endif /* MBEDTLS_VERSION_C */

programs/test/cmake_package/cmake_package.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
* linkage works, but that is all. */
1919
int main()
2020
{
21-
/* This version string is 18 bytes long, as advised by version.h. */
22-
char version[18];
23-
24-
mbedtls_version_get_string_full(version);
21+
const char *version = mbedtls_version_get_string_full();
2522

2623
mbedtls_printf("Built against %s\n", version);
2724

programs/test/cmake_package_install/cmake_package_install.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
* linkage works, but that is all. */
2020
int main()
2121
{
22-
/* This version string is 18 bytes long, as advised by version.h. */
23-
char version[18];
24-
25-
mbedtls_version_get_string_full(version);
22+
const char *version = mbedtls_version_get_string_full();
2623

2724
mbedtls_printf("Built against %s\n", version);
2825

programs/test/cmake_subproject/cmake_subproject.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
* linkage works, but that is all. */
2020
int main()
2121
{
22-
/* This version string is 18 bytes long, as advised by version.h. */
23-
char version[18];
24-
25-
mbedtls_version_get_string_full(version);
22+
const char *version = mbedtls_version_get_string_full();
2623

2724
mbedtls_printf("Built against %s\n", version);
2825

tests/psa-client-server/psasim/src/psa_sim_crypto_client.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ int psa_crypto_call(int function,
7373

7474
psa_status_t psa_crypto_init(void)
7575
{
76-
char mbedtls_version[18];
76+
const char *mbedtls_version;
7777
uint8_t *result = NULL;
7878
size_t result_length;
7979
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8080

81-
mbedtls_version_get_string_full(mbedtls_version);
81+
mbedtls_version = mbedtls_version_get_string_full();
8282
CLIENT_PRINT("%s", mbedtls_version);
8383

8484
CLIENT_PRINT("My PID: %d", getpid());

tests/psa-client-server/psasim/src/psa_sim_generate.pl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,12 @@ sub client_calls_header
390390
391391
psa_status_t psa_crypto_init(void)
392392
{
393-
char mbedtls_version[18];
393+
const char *mbedtls_version;
394394
uint8_t *result = NULL;
395395
size_t result_length;
396396
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
397397
398-
mbedtls_version_get_string_full(mbedtls_version);
398+
mbedtls_version = mbedtls_version_get_string_full();
399399
CLIENT_PRINT("%s", mbedtls_version);
400400
401401
CLIENT_PRINT("My PID: %d", getpid());

tests/psa-client-server/psasim/src/server.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ int psa_server_main(int argc, char *argv[])
5656
extern psa_status_t psa_crypto_close(void);
5757

5858
#if defined(MBEDTLS_VERSION_C)
59-
char mbedtls_version[18];
60-
mbedtls_version_get_string_full(mbedtls_version);
59+
const char *mbedtls_version = mbedtls_version_get_string_full();
6160
SERVER_PRINT("%s", mbedtls_version);
6261
#endif
6362

tests/suites/test_suite_version.function

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,17 @@ void check_compiletime_version(char *version_str)
3838
void check_runtime_version(char *version_str)
3939
{
4040
char build_str[100];
41-
char get_str[100];
41+
const char *get_str;
4242
char build_str_full[100];
43-
char get_str_full[100];
43+
const char *get_str_full;
4444
unsigned int get_int;
4545

4646
memset(build_str, 0, 100);
47-
memset(get_str, 0, 100);
4847
memset(build_str_full, 0, 100);
49-
memset(get_str_full, 0, 100);
5048

5149
get_int = mbedtls_version_get_number();
52-
mbedtls_version_get_string(get_str);
53-
mbedtls_version_get_string_full(get_str_full);
50+
get_str = mbedtls_version_get_string();
51+
get_str_full = mbedtls_version_get_string_full();
5452

5553
mbedtls_snprintf(build_str, 100, "%u.%u.%u",
5654
(get_int >> 24) & 0xFF,

0 commit comments

Comments
 (0)