From 6401c830ffcd82ee9c9e26255f2fadf7092c7321 Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Khan Date: Wed, 12 Feb 2025 09:01:27 -0800 Subject: [PATCH] Make aws_byte_cursor_from_string NULL tolerant (#1187) --- include/aws/common/string.h | 1 + source/string.c | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/aws/common/string.h b/include/aws/common/string.h index 37ec2f99e..1b969552e 100644 --- a/include/aws/common/string.h +++ b/include/aws/common/string.h @@ -325,6 +325,7 @@ bool aws_byte_buf_write_from_whole_string( /** * Creates an aws_byte_cursor from an existing string. + * If the src is NULL, it returns an empty cursor */ AWS_COMMON_API struct aws_byte_cursor aws_byte_cursor_from_string(const struct aws_string *src); diff --git a/source/string.c b/source/string.c index 2fd791230..e68f6b284 100644 --- a/source/string.c +++ b/source/string.c @@ -416,7 +416,12 @@ bool aws_byte_buf_write_from_whole_string( * Creates an aws_byte_cursor from an existing string. */ struct aws_byte_cursor aws_byte_cursor_from_string(const struct aws_string *src) { - AWS_PRECONDITION(aws_string_is_valid(src)); + if (!src) { + struct aws_byte_cursor cursor; + AWS_ZERO_STRUCT(cursor); + return cursor; + } + return aws_byte_cursor_from_array(aws_string_bytes(src), src->len); }