Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fsmonitor-settings: use xstrdup(), don't strdup() twice
Fix a memory leak and lack of error checking in 1e0ea5c (fsmonitor: config settings are repository-specific, 2022-03-25). Anything calling repo_config_get_pathname() will get a strbuf_detach()'d string, so by using strdup() unconditionally we'd needlessly duplicate a string that was already duplicated for us. This was seemingly being done because fsm_settings__set_hook() wanted to be a generic function that could take a path from anywhere, and even if it was only used within fsmonitor-settings.c it would also need to duplicate strings in the getenv() case. Then we didn't check the return value of strdup(), let's use xstrdup() instead. To fix this we'd need to pass information down to fsm_settings__set_hook() about whether to duplicate the string, but since nothing except this file uses it we can at least make it static. But if we were going to do that it wouldn't make any sense to keep the "!r" fallback case, we know we have "r" in lookup_fsmonitor_settings(). Nor would it make sense to have these "set" functions call lookup_fsmonitor_settings(), since their only caller is lookup_fsmonitor_settings() itself. And as we know that that's all true the FREE_AND_NULL() don't make any sense either, we'd only want that if we were clearing a previously set hook, but due to the same guard clause in lookup_fsmonitor_settings() that's keeping us from recusing infinitely we know that we won't ever re-set these values once they're set. So all of this was quite a a lot of boilerplate just to set a couple of variables, let's just do that in lookup_fsmonitor_settings() instead. While we're at it let's remove the fsm_settings__set_disabled() sibling method, it wasn't used by anything. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
- Loading branch information