From d3cda5b942808d49ee0b0e63853cda91c926c7ed Mon Sep 17 00:00:00 2001 From: Dmitriy Musatkin Date: Fri, 29 Mar 2024 16:35:39 -0700 Subject: [PATCH 1/5] make it smaller --- source/string.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/source/string.c b/source/string.c index a3d2c204e..b898fa683 100644 --- a/source/string.c +++ b/source/string.c @@ -189,11 +189,8 @@ struct aws_string *aws_string_new_from_c_str(struct aws_allocator *allocator, co struct aws_string *aws_string_new_from_array(struct aws_allocator *allocator, const uint8_t *bytes, size_t len) { AWS_PRECONDITION(allocator); AWS_PRECONDITION(AWS_MEM_IS_READABLE(bytes, len)); - size_t malloc_size; - if (aws_add_size_checked(sizeof(struct aws_string) + 1, len, &malloc_size)) { - return NULL; - } - struct aws_string *str = aws_mem_acquire(allocator, malloc_size); + + struct aws_string *str = aws_mem_acquire(allocator, offsetof(struct aws_string, bytes[len])); if (!str) { return NULL; } From 3c9ee28432e1cb2fcc5b7454bc7cf3c14c335fdf Mon Sep 17 00:00:00 2001 From: Dmitriy Musatkin Date: Fri, 29 Mar 2024 16:40:11 -0700 Subject: [PATCH 2/5] try this --- source/string.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/source/string.c b/source/string.c index b898fa683..79f4d96dd 100644 --- a/source/string.c +++ b/source/string.c @@ -3,6 +3,7 @@ * SPDX-License-Identifier: Apache-2.0. */ #include +#include #ifdef _WIN32 # include @@ -189,8 +190,16 @@ struct aws_string *aws_string_new_from_c_str(struct aws_allocator *allocator, co struct aws_string *aws_string_new_from_array(struct aws_allocator *allocator, const uint8_t *bytes, size_t len) { AWS_PRECONDITION(allocator); AWS_PRECONDITION(AWS_MEM_IS_READABLE(bytes, len)); + size_t malloc_size; + if (aws_add_size_checked(sizeof(struct aws_string) + 1, len, &malloc_size)) { + return NULL; + } + + size_t malloc_size2 = offsetof(struct aws_string, bytes[len]) + 1; + + AWS_LOGF_DEBUG(0, "foo %zu %zu", malloc_size, malloc_size2); - struct aws_string *str = aws_mem_acquire(allocator, offsetof(struct aws_string, bytes[len])); + struct aws_string *str = aws_mem_acquire(allocator, malloc_size2); if (!str) { return NULL; } From bfd4c1cab3ee70d17a92f19cafea33d500721ec0 Mon Sep 17 00:00:00 2001 From: Dmitriy Musatkin Date: Fri, 29 Mar 2024 16:47:44 -0700 Subject: [PATCH 3/5] remove unneeded stuff --- source/string.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/source/string.c b/source/string.c index 79f4d96dd..ffd0c3894 100644 --- a/source/string.c +++ b/source/string.c @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0. */ #include -#include #ifdef _WIN32 # include @@ -190,16 +189,10 @@ struct aws_string *aws_string_new_from_c_str(struct aws_allocator *allocator, co struct aws_string *aws_string_new_from_array(struct aws_allocator *allocator, const uint8_t *bytes, size_t len) { AWS_PRECONDITION(allocator); AWS_PRECONDITION(AWS_MEM_IS_READABLE(bytes, len)); - size_t malloc_size; - if (aws_add_size_checked(sizeof(struct aws_string) + 1, len, &malloc_size)) { - return NULL; - } - size_t malloc_size2 = offsetof(struct aws_string, bytes[len]) + 1; + size_t malloc_size = offsetof(struct aws_string, bytes[len]) + 1; - AWS_LOGF_DEBUG(0, "foo %zu %zu", malloc_size, malloc_size2); - - struct aws_string *str = aws_mem_acquire(allocator, malloc_size2); + struct aws_string *str = aws_mem_acquire(allocator, malloc_size); if (!str) { return NULL; } From 802a77b2268a0579c3da36993ac9e1246e4ba39c Mon Sep 17 00:00:00 2001 From: Dmitriy Musatkin Date: Fri, 29 Mar 2024 16:50:48 -0700 Subject: [PATCH 4/5] just inline it --- source/string.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/string.c b/source/string.c index ffd0c3894..0e4e50c85 100644 --- a/source/string.c +++ b/source/string.c @@ -190,9 +190,7 @@ struct aws_string *aws_string_new_from_array(struct aws_allocator *allocator, co AWS_PRECONDITION(allocator); AWS_PRECONDITION(AWS_MEM_IS_READABLE(bytes, len)); - size_t malloc_size = offsetof(struct aws_string, bytes[len]) + 1; - - struct aws_string *str = aws_mem_acquire(allocator, malloc_size); + struct aws_string *str = aws_mem_acquire(allocator, offsetof(struct aws_string, bytes[len]) + 1); if (!str) { return NULL; } From d36969c023bf99cc5b8397ea0a51fe895405dcda Mon Sep 17 00:00:00 2001 From: Dmitriy Musatkin Date: Mon, 1 Apr 2024 00:32:24 -0700 Subject: [PATCH 5/5] move inside --- source/string.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/string.c b/source/string.c index 0e4e50c85..2fd791230 100644 --- a/source/string.c +++ b/source/string.c @@ -190,7 +190,7 @@ struct aws_string *aws_string_new_from_array(struct aws_allocator *allocator, co AWS_PRECONDITION(allocator); AWS_PRECONDITION(AWS_MEM_IS_READABLE(bytes, len)); - struct aws_string *str = aws_mem_acquire(allocator, offsetof(struct aws_string, bytes[len]) + 1); + struct aws_string *str = aws_mem_acquire(allocator, offsetof(struct aws_string, bytes[len + 1])); if (!str) { return NULL; }