From e3e7ccd35a85f9cd38c67cb1988251f1543b6632 Mon Sep 17 00:00:00 2001 From: Michael Graeb Date: Mon, 28 Oct 2019 15:47:07 -0700 Subject: [PATCH] aws_string_c_str() (#534) --- include/aws/common/string.h | 6 ++++++ include/aws/common/string.inl | 9 +++++++++ source/posix/environment.c | 6 +++--- source/windows/environment.c | 6 +++--- tests/logging/log_formatter_test.c | 2 +- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/include/aws/common/string.h b/include/aws/common/string.h index 0fdf81a0c..86d248793 100644 --- a/include/aws/common/string.h +++ b/include/aws/common/string.h @@ -217,6 +217,12 @@ int aws_secure_strlen(const char *str, size_t max_read_len, size_t *str_len); AWS_STATIC_IMPL const uint8_t *aws_string_bytes(const struct aws_string *str); +/** + * Equivalent to `(const char *)str->bytes`. + */ +AWS_STATIC_IMPL +const char *aws_string_c_str(const struct aws_string *str); + /** * Evaluates the set of properties that define the shape of all valid aws_string structures. * It is also a cheap check, in the sense it run in constant time (i.e., no loops or recursion). diff --git a/include/aws/common/string.inl b/include/aws/common/string.inl index 4834458d4..32a826993 100644 --- a/include/aws/common/string.inl +++ b/include/aws/common/string.inl @@ -27,6 +27,15 @@ const uint8_t *aws_string_bytes(const struct aws_string *str) { return str->bytes; } +/** + * Equivalent to `(const char *)str->bytes`. + */ +AWS_STATIC_IMPL +const char *aws_string_c_str(const struct aws_string *str) { + AWS_PRECONDITION(aws_string_is_valid(str)); + return (const char *)str->bytes; +} + /** * Evaluates the set of properties that define the shape of all valid aws_string structures. * It is also a cheap check, in the sense it run in constant time (i.e., no loops or recursion). diff --git a/source/posix/environment.c b/source/posix/environment.c index a069c30d0..90a05cd32 100644 --- a/source/posix/environment.c +++ b/source/posix/environment.c @@ -23,7 +23,7 @@ int aws_get_environment_value( const struct aws_string *variable_name, struct aws_string **value_out) { - const char *value = getenv((const char *)variable_name->bytes); + const char *value = getenv(aws_string_c_str(variable_name)); if (value == NULL) { *value_out = NULL; return AWS_OP_SUCCESS; @@ -39,7 +39,7 @@ int aws_get_environment_value( int aws_set_environment_value(const struct aws_string *variable_name, const struct aws_string *value) { - if (setenv((const char *)variable_name->bytes, (const char *)value->bytes, 1) != 0) { + if (setenv(aws_string_c_str(variable_name), aws_string_c_str(value), 1) != 0) { return aws_raise_error(AWS_ERROR_ENVIRONMENT_SET); } @@ -47,7 +47,7 @@ int aws_set_environment_value(const struct aws_string *variable_name, const stru } int aws_unset_environment_value(const struct aws_string *variable_name) { - if (unsetenv((const char *)variable_name->bytes) != 0) { + if (unsetenv(aws_string_c_str(variable_name)) != 0) { return aws_raise_error(AWS_ERROR_ENVIRONMENT_UNSET); } diff --git a/source/windows/environment.c b/source/windows/environment.c index 265f02b1c..8c042ddc5 100644 --- a/source/windows/environment.c +++ b/source/windows/environment.c @@ -26,7 +26,7 @@ int aws_get_environment_value( #pragma warning(push) #pragma warning(disable : 4996) - const char *value = getenv((const char *)variable_name->bytes); + const char *value = getenv(aws_string_c_str(variable_name)); #pragma warning(pop) @@ -44,7 +44,7 @@ int aws_get_environment_value( } int aws_set_environment_value(const struct aws_string *variable_name, const struct aws_string *value) { - if (_putenv_s((const char *)variable_name->bytes, (const char *)value->bytes) != 0) { + if (_putenv_s(aws_string_c_str(variable_name), aws_string_c_str(value)) != 0) { return aws_raise_error(AWS_ERROR_ENVIRONMENT_SET); } @@ -54,7 +54,7 @@ int aws_set_environment_value(const struct aws_string *variable_name, const stru AWS_STATIC_STRING_FROM_LITERAL(s_empty_string, ""); int aws_unset_environment_value(const struct aws_string *variable_name) { - if (_putenv_s((const char *)variable_name->bytes, (const char *)s_empty_string->bytes) != 0) { + if (_putenv_s(aws_string_c_str(variable_name), aws_string_c_str(s_empty_string)) != 0) { return aws_raise_error(AWS_ERROR_ENVIRONMENT_UNSET); } diff --git a/tests/logging/log_formatter_test.c b/tests/logging/log_formatter_test.c index 2db2ef72b..7c8111627 100644 --- a/tests/logging/log_formatter_test.c +++ b/tests/logging/log_formatter_test.c @@ -48,7 +48,7 @@ int do_default_log_formatter_test( aws_log_formatter_clean_up(&formatter); char buffer[TEST_FORMATTER_MAX_BUFFER_SIZE]; - snprintf(buffer, TEST_FORMATTER_MAX_BUFFER_SIZE, "%s", (const char *)output->bytes); + snprintf(buffer, TEST_FORMATTER_MAX_BUFFER_SIZE, "%s", aws_string_c_str(output)); aws_string_destroy(output);