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.
What do you do in this case?
Put the conditional in the code and don't worry about it.
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(¶m, "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"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments