]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
strings.h: Ensure ast_str_buffer(…) returns a 0 terminated string.
authorSean Bright <sean@seanbright.com>
Sat, 17 Feb 2024 19:41:38 +0000 (14:41 -0500)
committerSean Bright <sean@seanbright.com>
Fri, 23 Feb 2024 16:39:23 +0000 (16:39 +0000)
If a dynamic string is created with an initial length of 0,
`ast_str_buffer(…)` will return an invalid pointer.

This was a secondary discovery when fixing #65.

include/asterisk/strings.h

index 10eb011463803879d790d08f43021b905089eda2..935c7e9236f25792c4349bc242c79a44994791e7 100644 (file)
@@ -753,7 +753,10 @@ char * attribute_pure ast_str_buffer(const struct ast_str *buf),
         * being returned; eventually, it should become truly const
         * and only be modified via accessor functions
         */
-       return (char *) buf->__AST_STR_STR;
+       if (__builtin_expect(buf->__AST_STR_LEN > 0, 1)) {
+               return (char *) buf->__AST_STR_STR;
+       }
+       return "";
 }
 )