Skip to content

[UDF] StringVal::operator==() should avoid pass NULL to memcmp() #3843

@vagetablechicken

Description

@vagetablechicken

Describe the bug
A clear and concise description of what the bug is.
https://github.com/apache/incubator-doris/blob/e16873a6c1bbf0cf9e97f7a7bfd60b162eea4e31/be/src/udf/udf.h#L634-L648
StringVal::operator==() will check is_null, len & content(*ptr).
But when a StringVal obj sval len==0, the ptr can be NULL. It's possible to pass NULL ptr to memcmp(). Ref https://en.cppreference.com/w/c/string/byte/memcmp, The behavior is undefined if either lhs or rhs is a null pointer.
It's better to fix it.

StringVal StringFunctions::substring(
        FunctionContext* context, const StringVal& str,
        const IntVal& pos, const IntVal& len)

StringFunctions::substring() will return StringVal() when arg len == 0, StringVal() will set ptr to NULL, len to 0.
If we don't want to give a len == 0 StringVal an empty string('\0'), we should skip memcmp() when lhs or rhs len == 0.

To Reproduce
Steps to reproduce the behavior:

  1. compare StringVal(len=0, ptr="") with StringVal().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions