From afff514ca756943e69f5a9f19e44eadf1726e01d Mon Sep 17 00:00:00 2001 From: akallabeth Date: Thu, 12 Sep 2024 17:07:16 +0200 Subject: [PATCH] [compiler] add WINPR_ATTR_MALLOC Add WINPR_ATTR_MALLOC checks to allocating functions to help compilers find memory leaks or allocation mismatches --- include/freerdp/assistance.h | 8 ++++++ include/freerdp/codec/audio.h | 2 +- include/freerdp/codec/color.h | 2 ++ include/freerdp/crypto/certificate.h | 22 +++++++++++++++++ include/freerdp/server/proxy/proxy_config.h | 17 +++++++------ include/freerdp/settings.h | 27 +++++++++++++-------- include/freerdp/utils/aad.h | 1 + include/freerdp/utils/http.h | 2 +- include/freerdp/utils/passphrase.h | 4 +-- include/freerdp/utils/string.h | 6 +++-- libfreerdp/codec/audio.c | 2 +- libfreerdp/common/settings.c | 2 +- libfreerdp/crypto/certificate.h | 1 + libfreerdp/utils/http.c | 2 +- libfreerdp/utils/passphrase.c | 8 +++--- libfreerdp/utils/string.c | 4 +-- 16 files changed, 78 insertions(+), 32 deletions(-) diff --git a/include/freerdp/assistance.h b/include/freerdp/assistance.h index f6e9aa9f6..e38e58b77 100644 --- a/include/freerdp/assistance.h +++ b/include/freerdp/assistance.h @@ -31,11 +31,19 @@ extern "C" typedef struct rdp_assistance_file rdpAssistanceFile; + WINPR_ATTR_MALLOC(free, 1) FREERDP_API BYTE* freerdp_assistance_hex_string_to_bin(const void* str, size_t* size); + + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_assistance_bin_to_hex_string(const void* data, size_t size); + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_assistance_generate_pass_stub(DWORD flags); + + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_assistance_construct_expert_blob(const char* name, const char* pass); + + WINPR_ATTR_MALLOC(free, 1) FREERDP_API BYTE* freerdp_assistance_encrypt_pass_stub(const char* password, const char* passStub, size_t* pEncryptedSize); diff --git a/include/freerdp/codec/audio.h b/include/freerdp/codec/audio.h index f5e5040b2..fd4b1f9bb 100644 --- a/include/freerdp/codec/audio.h +++ b/include/freerdp/codec/audio.h @@ -205,7 +205,7 @@ extern "C" FREERDP_API UINT32 audio_format_compute_time_length(const AUDIO_FORMAT* format, size_t size); - FREERDP_API char* audio_format_get_tag_string(UINT16 wFormatTag); + FREERDP_API const char* audio_format_get_tag_string(UINT16 wFormatTag); FREERDP_API void audio_format_print(wLog* log, DWORD level, const AUDIO_FORMAT* format); FREERDP_API void audio_formats_print(wLog* log, DWORD level, const AUDIO_FORMAT* formats, diff --git a/include/freerdp/codec/color.h b/include/freerdp/codec/color.h index 35ed3ebc6..1612ad4e2 100644 --- a/include/freerdp/codec/color.h +++ b/include/freerdp/codec/color.h @@ -22,6 +22,7 @@ #ifndef FREERDP_CODEC_COLOR_H #define FREERDP_CODEC_COLOR_H +#include #include #ifdef __cplusplus @@ -286,6 +287,7 @@ typedef struct gdi_palette gdiPalette; * @return A buffer allocated with winpr_aligned_malloc(width * height, 16) * if successful, NULL otherwise. */ + WINPR_ATTR_MALLOC(winpr_aligned_free, 1) FREERDP_API BYTE* freerdp_glyph_convert(UINT32 width, UINT32 height, const BYTE* WINPR_RESTRICT data); diff --git a/include/freerdp/crypto/certificate.h b/include/freerdp/crypto/certificate.h index 7a8f43403..d632d3957 100644 --- a/include/freerdp/crypto/certificate.h +++ b/include/freerdp/crypto/certificate.h @@ -54,15 +54,23 @@ extern "C" FREERDP_API BOOL freerdp_certificate_is_rsa(const rdpCertificate* certificate); + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_hash(const rdpCertificate* certificate, const char* hash, size_t* plength); + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_fingerprint_by_hash(const rdpCertificate* certificate, const char* hash); + + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_fingerprint_by_hash_ex(const rdpCertificate* certificate, const char* hash, BOOL separator); + + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_fingerprint(const rdpCertificate* certificate); + + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_pem(const rdpCertificate* certificate, size_t* pLength); @@ -75,15 +83,24 @@ extern "C" * @return A newly allocated string containing the requested PEM (free to deallocate) or NULL * @since version 3.8.0 */ + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_pem_ex(const rdpCertificate* certificate, size_t* pLength, BOOL withCertChain); + + WINPR_ATTR_MALLOC(free, 1) FREERDP_API BYTE* freerdp_certificate_get_der(const rdpCertificate* certificate, size_t* pLength); + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_subject(const rdpCertificate* certificate); + + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_issuer(const rdpCertificate* certificate); + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_upn(const rdpCertificate* certificate); + + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_email(const rdpCertificate* certificate); /** @@ -93,13 +110,17 @@ extern "C" * @return A newly allocated string containing the date, use \b free to deallocate * @since version 3.8.0 */ + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_validity(const rdpCertificate* certificate, BOOL startDate); FREERDP_API WINPR_MD_TYPE freerdp_certificate_get_signature_alg(const rdpCertificate* cert); + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_common_name(const rdpCertificate* cert, size_t* plength); + + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char** freerdp_certificate_get_dns_names(const rdpCertificate* cert, size_t* pcount, size_t** pplengths); FREERDP_API void freerdp_certificate_free_dns_names(size_t count, size_t* lengths, @@ -115,6 +136,7 @@ extern "C" FREERDP_API BOOL freerdp_certificate_is_rdp_security_compatible(const rdpCertificate* cert); + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_certificate_get_param(const rdpCertificate* cert, enum FREERDP_CERT_PARAM what, size_t* psize); diff --git a/include/freerdp/server/proxy/proxy_config.h b/include/freerdp/server/proxy/proxy_config.h index d6b50caea..5441b88f6 100644 --- a/include/freerdp/server/proxy/proxy_config.h +++ b/include/freerdp/server/proxy/proxy_config.h @@ -126,6 +126,13 @@ extern "C" */ FREERDP_API BOOL pf_server_config_dump(const char* file); + /** + * @brief pf_server_config_free Releases all resources associated with proxyConfig + * + * @param config A pointer to the proxyConfig to clean up. Might be NULL. + */ + FREERDP_API void pf_server_config_free(proxyConfig* config); + /** * @brief server_config_load_ini Create a proxyConfig from a already loaded * INI file. @@ -134,6 +141,7 @@ extern "C" * * @return A proxyConfig or NULL in case of failure. */ + WINPR_ATTR_MALLOC(pf_server_config_free, 1) FREERDP_API proxyConfig* server_config_load_ini(wIniFile* ini); /** * @brief pf_server_config_load_file Create a proxyConfig from a INI file found at path. @@ -142,6 +150,7 @@ extern "C" * * @return A proxyConfig or NULL in case of failure. */ + WINPR_ATTR_MALLOC(pf_server_config_free, 1) FREERDP_API proxyConfig* pf_server_config_load_file(const char* path); /** @@ -152,6 +161,7 @@ extern "C" * * @return A proxyConfig or NULL in case of failure. */ + WINPR_ATTR_MALLOC(pf_server_config_free, 1) FREERDP_API proxyConfig* pf_server_config_load_buffer(const char* buffer); /** @@ -161,13 +171,6 @@ extern "C" */ FREERDP_API void pf_server_config_print(const proxyConfig* config); - /** - * @brief pf_server_config_free Releases all resources associated with proxyConfig - * - * @param config A pointer to the proxyConfig to clean up. Might be NULL. - */ - FREERDP_API void pf_server_config_free(proxyConfig* config); - /** * @brief pf_config_required_plugins_count * diff --git a/include/freerdp/settings.h b/include/freerdp/settings.h index 527cd96ca..355069e70 100644 --- a/include/freerdp/settings.h +++ b/include/freerdp/settings.h @@ -80,6 +80,12 @@ extern "C" #define FREERDP_SETTINGS_SERVER_MODE 0x00000001 #define FREERDP_SETTINGS_REMOTE_MODE 0x00000002 + /** \brief Free a settings struct with all data in it + * + * \param settings A pointer to the settings to free, May be NULL + */ + FREERDP_API void freerdp_settings_free(rdpSettings* settings); + /** \brief creates a new setting struct * * \param flags Flags for creation, use \b FREERDP_SETTINGS_SERVER_MODE for server settings, 0 @@ -87,6 +93,7 @@ extern "C" * * \return A newly allocated settings struct or NULL */ + WINPR_ATTR_MALLOC(freerdp_settings_free, 1) FREERDP_API rdpSettings* freerdp_settings_new(DWORD flags); /** \brief Creates a deep copy of settings @@ -95,6 +102,7 @@ extern "C" * * \return A newly allocated copy of \b settings or NULL */ + WINPR_ATTR_MALLOC(freerdp_settings_free, 1) FREERDP_API rdpSettings* freerdp_settings_clone(const rdpSettings* settings); /** \brief Deep copies settings from \b src to \b dst @@ -122,12 +130,6 @@ extern "C" FREERDP_API BOOL freerdp_settings_copy_item(rdpSettings* dst, const rdpSettings* src, SSIZE_T id); - /** \brief Free a settings struct with all data in it - * - * \param settings A pointer to the settings to free, May be NULL - */ - FREERDP_API void freerdp_settings_free(rdpSettings* settings); - /** \brief Dumps the contents of a settings struct to a WLog logger * * \param log The logger to write to, must not be NULL @@ -148,10 +150,14 @@ extern "C" FREERDP_API BOOL freerdp_settings_print_diff(wLog* log, DWORD level, const rdpSettings* src, const rdpSettings* other); - FREERDP_API ADDIN_ARGV* freerdp_addin_argv_new(size_t argc, const char* argv[]); - FREERDP_API ADDIN_ARGV* freerdp_addin_argv_clone(const ADDIN_ARGV* args); FREERDP_API void freerdp_addin_argv_free(ADDIN_ARGV* args); + WINPR_ATTR_MALLOC(freerdp_addin_argv_free, 1) + FREERDP_API ADDIN_ARGV* freerdp_addin_argv_new(size_t argc, const char* argv[]); + + WINPR_ATTR_MALLOC(freerdp_addin_argv_free, 1) + FREERDP_API ADDIN_ARGV* freerdp_addin_argv_clone(const ADDIN_ARGV* args); + FREERDP_API BOOL freerdp_addin_argv_add_argument(ADDIN_ARGV* args, const char* argument); FREERDP_API BOOL freerdp_addin_argv_add_argument_ex(ADDIN_ARGV* args, const char* argument, size_t len); @@ -686,8 +692,8 @@ extern "C" * * \return A pointer to \b buffer for success, NULL otherwise */ - FREERDP_API char* freerdp_rail_support_flags_to_string(UINT32 flags, char* buffer, - size_t length); + FREERDP_API const char* freerdp_rail_support_flags_to_string(UINT32 flags, char* buffer, + size_t length); /** \brief Returns a stringified representation of the RDP protocol version. * @@ -721,6 +727,7 @@ extern "C" * @return The current configuration path or \b NULL * @since version 3.6.0 */ + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_settings_get_config_path(void); #ifdef __cplusplus diff --git a/include/freerdp/utils/aad.h b/include/freerdp/utils/aad.h index 1c69a3fc9..4d65a9130 100644 --- a/include/freerdp/utils/aad.h +++ b/include/freerdp/utils/aad.h @@ -40,6 +40,7 @@ * * @return The token string or \b NULL */ +WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_utils_aad_get_access_token(wLog* log, const char* data, size_t length); #endif diff --git a/include/freerdp/utils/http.h b/include/freerdp/utils/http.h index 9719402df..b2e643ebf 100644 --- a/include/freerdp/utils/http.h +++ b/include/freerdp/utils/http.h @@ -70,6 +70,6 @@ FREERDP_API BOOL freerdp_http_request(const char* url, const char* body, long* s BYTE** response, size_t* response_length); FREERDP_API const char* freerdp_http_status_string(long status); -FREERDP_API char* freerdp_http_status_string_format(long status, char* buffer, size_t size); +FREERDP_API const char* freerdp_http_status_string_format(long status, char* buffer, size_t size); #endif /* FREERDP_UTILS_HTTP_H */ diff --git a/include/freerdp/utils/passphrase.h b/include/freerdp/utils/passphrase.h index f6a0f9605..0bcce1953 100644 --- a/include/freerdp/utils/passphrase.h +++ b/include/freerdp/utils/passphrase.h @@ -34,8 +34,8 @@ extern "C" FREERDP_API int freerdp_interruptible_getc(rdpContext* context, FILE* file); FREERDP_API SSIZE_T freerdp_interruptible_get_line(rdpContext* context, char** lineptr, size_t* size, FILE* stream); - FREERDP_API char* freerdp_passphrase_read(rdpContext* context, const char* prompt, char* buf, - size_t bufsiz, int from_stdin); + FREERDP_API const char* freerdp_passphrase_read(rdpContext* context, const char* prompt, + char* buf, size_t bufsiz, int from_stdin); #ifdef __cplusplus } diff --git a/include/freerdp/utils/string.h b/include/freerdp/utils/string.h index c0083684f..1268360a0 100644 --- a/include/freerdp/utils/string.h +++ b/include/freerdp/utils/string.h @@ -30,8 +30,10 @@ extern "C" { #endif - FREERDP_API char* rdp_redirection_flags_to_string(UINT32 flags, char* buffer, size_t size); - FREERDP_API char* rdp_cluster_info_flags_to_string(UINT32 flags, char* buffer, size_t size); + FREERDP_API const char* rdp_redirection_flags_to_string(UINT32 flags, char* buffer, + size_t size); + FREERDP_API const char* rdp_cluster_info_flags_to_string(UINT32 flags, char* buffer, + size_t size); #ifdef __cplusplus } diff --git a/libfreerdp/codec/audio.c b/libfreerdp/codec/audio.c index 3791fd81a..b59837803 100644 --- a/libfreerdp/codec/audio.c +++ b/libfreerdp/codec/audio.c @@ -75,7 +75,7 @@ UINT32 audio_format_compute_time_length(const AUDIO_FORMAT* format, size_t size) return mstime; } -char* audio_format_get_tag_string(UINT16 wFormatTag) +const char* audio_format_get_tag_string(UINT16 wFormatTag) { switch (wFormatTag) { diff --git a/libfreerdp/common/settings.c b/libfreerdp/common/settings.c index ef79a1c73..327ad2efb 100644 --- a/libfreerdp/common/settings.c +++ b/libfreerdp/common/settings.c @@ -1948,7 +1948,7 @@ BOOL freerdp_device_equal(const RDPDR_DEVICE* what, const RDPDR_DEVICE* expect) return TRUE; } -char* freerdp_rail_support_flags_to_string(UINT32 flags, char* buffer, size_t length) +const char* freerdp_rail_support_flags_to_string(UINT32 flags, char* buffer, size_t length) { const UINT32 mask = RAIL_LEVEL_SUPPORTED | RAIL_LEVEL_DOCKED_LANGBAR_SUPPORTED | diff --git a/libfreerdp/crypto/certificate.h b/libfreerdp/crypto/certificate.h index ead71f8ee..57c0dcb85 100644 --- a/libfreerdp/crypto/certificate.h +++ b/libfreerdp/crypto/certificate.h @@ -49,6 +49,7 @@ FREERDP_LOCAL BOOL freerdp_certificate_read_server_cert(rdpCertificate* certific FREERDP_LOCAL SSIZE_T freerdp_certificate_write_server_cert(const rdpCertificate* certificate, UINT32 dwVersion, wStream* s); +WINPR_ATTR_MALLOC(freerdp_certificate_free, 1) FREERDP_LOCAL rdpCertificate* freerdp_certificate_clone(const rdpCertificate* certificate); FREERDP_LOCAL const rdpCertInfo* freerdp_certificate_get_info(const rdpCertificate* certificate); diff --git a/libfreerdp/utils/http.c b/libfreerdp/utils/http.c index 0f50823b8..aa14677a9 100644 --- a/libfreerdp/utils/http.c +++ b/libfreerdp/utils/http.c @@ -384,7 +384,7 @@ const char* freerdp_http_status_string(long status) } } -char* freerdp_http_status_string_format(long status, char* buffer, size_t size) +const char* freerdp_http_status_string_format(long status, char* buffer, size_t size) { const char* code = freerdp_http_status_string(status); (void)_snprintf(buffer, size, "%s [%ld]", code, status); diff --git a/libfreerdp/utils/passphrase.c b/libfreerdp/utils/passphrase.c index 9ca51bd17..0388fba59 100644 --- a/libfreerdp/utils/passphrase.c +++ b/libfreerdp/utils/passphrase.c @@ -213,8 +213,8 @@ error: } } -static char* freerdp_passphrase_read_askpass(const char* prompt, char* buf, size_t bufsiz, - char const* askpass_env) +static const char* freerdp_passphrase_read_askpass(const char* prompt, char* buf, size_t bufsiz, + char const* askpass_env) { char command[4096] = { 0 }; @@ -234,8 +234,8 @@ static char* freerdp_passphrase_read_askpass(const char* prompt, char* buf, size return buf; } -char* freerdp_passphrase_read(rdpContext* context, const char* prompt, char* buf, size_t bufsiz, - int from_stdin) +const char* freerdp_passphrase_read(rdpContext* context, const char* prompt, char* buf, + size_t bufsiz, int from_stdin) { const char* askpass_env = getenv("FREERDP_ASKPASS"); diff --git a/libfreerdp/utils/string.c b/libfreerdp/utils/string.c index 4e520ea1d..2b5826f0b 100644 --- a/libfreerdp/utils/string.c +++ b/libfreerdp/utils/string.c @@ -22,7 +22,7 @@ #include #include -char* rdp_redirection_flags_to_string(UINT32 flags, char* buffer, size_t size) +const char* rdp_redirection_flags_to_string(UINT32 flags, char* buffer, size_t size) { struct map_t { @@ -60,7 +60,7 @@ char* rdp_redirection_flags_to_string(UINT32 flags, char* buffer, size_t size) return buffer; } -char* rdp_cluster_info_flags_to_string(UINT32 flags, char* buffer, size_t size) +const char* rdp_cluster_info_flags_to_string(UINT32 flags, char* buffer, size_t size) { const UINT32 version = (flags & ServerSessionRedirectionVersionMask) >> 2; if (flags & REDIRECTION_SUPPORTED)