Don't crash if NULL is passed to mrb_close#2951
Conversation
Sometimes it is very useful just return from mrb_close if NULL is passed as mrb. This is the same spirit of free(3), which just does nothing if NULL is passed.
|
Should we change the semantics of |
I'm not opposing merging this but note that isn't true for @franckverrot: Why? |
|
@cremno Thinking in terms of side-effects, I think it's a good thing to avoid crashes when we call |
|
do you have any use-case in mind? |
|
My usecase is that when mrb_state is a data field of C++ struct and it could be NULL, then we don't have check whether it is NULL in destructor: struct Foo {
Foo(...) : state(nullptr) {
if (some_condition_met()) {
state = mrb_open();
}
}
~Foo() {
mrb_close(mrb);
}
mrb_state *state;
};As commented earlier, many library functions, like fclose or SSL_free from OpenSSL, will crash if NULL is passed. In those functions, we have to check the argument is NULL or not. Many programs will do this for safety. If it is done in mrb_close, we can save those lines of code. |
|
👍 I think RAII is good Idiom. and want to Error output.
@tatsuhiro-t typo? s/mrb_close(mrb);/mrb_close(state)/g |
|
OK, adding 1 line would not harm. |
Don't crash if NULL is passed to mrb_close
|
@matz Isn't a good case to use If not, would you be OK to change the semantics of
It'd be the best of both worlds: don't check what you pass to |
|
Either Everything else would just be complicated. Simply check before the call if that info is needed for some reason. |
|
I'm thinking mrb_close should not be calm for NULL. As you said, free(NULL) is, but fclose(NULL) is not. |
Sometimes it is very useful just return from mrb_close if NULL is
passed as mrb. This is the same spirit of free(3), which just does
nothing if NULL is passed.