mirror of
https://github.com/shadowsocks/shadowsocks-libev.git
synced 2026-10-19 12:03:43 +00:00
find_library's search order puts CMAKE_PREFIX_PATH ahead of HINTS, so on a Homebrew machine with both mbedtls (4.x) and the keg-only mbedtls@3, the headers resolved to mbedtls@3 while the libraries resolved to 4.x. That is not a link error but memory corruption at runtime: mbedtls_cipher_context_t grew between the two versions, so every context allocated with ss_malloc(sizeof(cipher_evp_t)) from the 3.x headers was overrun by 4.x's mbedtls_cipher_init. It corrupted the heap in every binary that sets up a cipher, and made the unit tests segfault inside an unrelated malloc. Pick a prefix that provides both the headers and libmbedcrypto and pin every component to it, then verify at configure time that the header version string matches what the linked library reports, so a mismatch fails the build instead of corrupting memory.
30 lines
723 B
C
30 lines
723 B
C
/*
|
|
* Configure-time check that the mbedTLS headers and the linked library are
|
|
* the same version. A mismatch is not a link error, but struct layouts
|
|
* differ across major versions, so contexts sized with sizeof() from the
|
|
* headers are overrun by library code that expects the other layout.
|
|
*
|
|
* Exits 0 when the compile-time and runtime versions agree.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include <mbedtls/version.h>
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
char runtime[32] = { 0 };
|
|
|
|
mbedtls_version_get_string(runtime);
|
|
|
|
if (strcmp(runtime, MBEDTLS_VERSION_STRING) != 0) {
|
|
printf("headers say %s, library reports %s",
|
|
MBEDTLS_VERSION_STRING, runtime);
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|