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); }