Checking for NULL inside inline function

Mati

Consider the following code:

static inline StatusCode SomeHelperFunction(SomeType* arg)
{
    return OtherFunction(&arg->member, "foo", "bar");
}

static StatusCode OtherFunction(SomeType2* param, const char* param2, const char* param3)
{
    if (param == NULL) /* return error status code */
    
    /* do some stuff and return OK status code */
}

The problem is that OtherFunction cannot take NULL (and it checks for it and returns correct instance of StatusCode. With given helper function I cannot ensure that arg is not null and dereferencing it would not crash the application. So null guard inside SomeHelperFunction seems reasonable. But it is inline function and if statement could break it. There are two options I think.

  1. Do not check for null and just let the world burn
  2. Do check and possibly break the inline benefits (what if I have 100 functions like that)

What do you do in this case?

Bill Lynch

TL;DR:

Put the conditional in the code and don't worry about it.

Compilers are powerful:

Consider the code:

static int SomeHelperFunction(int* arg) {
    return Foo(&arg, "foo", "bar");
}

static inline int SomeInlineHelperFunction(int* param) {
    if (param == NULL)
        return 3;
    return Foo(&param, "foo", "bar");
}

void entrypoint() {
    int x;
    SomeHelperFunction(&x);
    SomeInlineHelperFunction(&x);
}

clang will inline SomeInlineHelperFunction and SomeHelperFunction, and it will even optimize away the null check since it can determine that it is impossible to be false.

entrypoint():
        push    rbx
        sub     rsp, 16
        lea     rbx, [rsp + 12]
        mov     qword ptr [rsp], rbx
        mov     rdi, rsp
        mov     esi, offset .L.str
        mov     edx, offset .L.str.1
        call    Foo(int**, char const*, char const*)
        mov     qword ptr [rsp], rbx
        mov     rdi, rsp
        mov     esi, offset .L.str
        mov     edx, offset .L.str.1
        call    Foo(int**, char const*, char const*)
        add     rsp, 16
        pop     rbx
        ret
.L.str:
        .asciz  "foo"

.L.str.1:
        .asciz  "bar"

https://godbolt.org/z/zYfds8

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

FirstOrDefault inline null checking

Checking null value inside function returns wrong result

Checking Function inside IF statement explanation

Kotlin inline function for checking if user is autheticated or not?

Checking if all values are null inside an initialised object

php checking if null with @if inside @foreach loop

How to create "inline if statement" with expressions in dynamic select for null checking

SQL nested if inside of a function with exist and checking timestamp

Convert null in "" inside a function

Typescript Object is possibly null even inside an if statement checking that the object is not null

javascript inline comment inside function call

inline CLOSURE/Anonymous function inside string?

If statement inside an inline styling function in React JS

Calling an Inline Function on a webform inside of mvc application

Checking for null when calling a function in JavaScript shortcut?

Typescript can not infer undefined or null checking in function

why Kotlin inline function params is must not be null

Null checking inside where clause in Entity Framework Linq

typescript type of a property inside an optional property in strict null checking

Checking for null in an instance function gets "Object possibly null" error

this becomes null inside the javascript function

Null a dictionary inside a function in python

Why does a function called inside a inline function not require definition?

Checking curl response code and returning results from inside a bash function

Checking the visibility of a div not working inside ajax success function

Checking script command-line arguments from inside a function

C++ inline function inside a static class or namespace

F# FSI does not hit break inside inline function

Immutable val null check inside extension function