-
-
Notifications
You must be signed in to change notification settings - Fork 396
Remove Global/Static variables in path.c to make it thread safe #5148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files
... and 10 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
librz/util/path.c
Outdated
static char *portable_prefix = NULL; | ||
static bool portable_prefix_searched = false; | ||
static RzThreadLock *portable_prefix_mutex = NULL; | ||
RzPathPortable *rz_portable; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this global variable and add the struct to RzCore.
Remove constructor/destructor and it's header file and just initialize/finalize RzPath within RzCore init/fini function
librz/util/path.c
Outdated
@@ -92,15 +88,16 @@ static char *set_portable_prefix(void) { | |||
*/ | |||
RZ_API void rz_path_set_prefix(RZ_NONNULL const char *path) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add RzPath as argument of this function
librz/include/rz_util/rz_path.h
Outdated
char *prefix; | ||
bool prefix_searched; | ||
RzThreadLock *prefix_mutex; | ||
} RzPathPortable; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call this just RzPath and make it a private structure. (see how RzDiff is defined)
librz/util/path.c
Outdated
} | ||
p->prefix = NULL; | ||
p->prefix_searched = false; | ||
p->prefix_mutex = rz_th_lock_new(recursive); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pass false instead of an argument like you did.
librz/util/path.c
Outdated
* \brief Return new RzPathPortable | ||
* \return New RzPathPortable | ||
*/ | ||
RZ_API RZ_OWN RzPathPortable *rz_path_portable_new(bool recursive) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the argument
librz/util/path.c
Outdated
return NULL; | ||
} | ||
RZ_FREE(p->prefix); | ||
p->prefix_searched = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need, remove this line
librz/util/path.c
Outdated
if (!p) { | ||
return NULL; | ||
} | ||
RZ_FREE(p->prefix); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call free. Use RZ_FREE/RZ_FREE_CUSTOM only if you intend to reuse the variables immediately afterward.
If the structure is then freed, there is no need to waste time setting them to NULL like these macros do
librz/util/path.c
Outdated
* \brief Deallocate memory associated with a RzPathPortable object | ||
* \return NULL | ||
*/ | ||
RZ_API RZ_OWN RzPathPortable *rz_path_portable_free(RzPathPortable *p) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return void
librz/util/path.c
Outdated
p->prefix_searched = false; | ||
RZ_FREE_CUSTOM(p->prefix_mutex, rz_th_lock_free); | ||
free(p); | ||
return NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this.
Not Ready for merging yet |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good progress
librz/egg/egg_lang.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this file, just create it's own path struct within RzEgg like you did for RzCore
librz/include/rz_util/rz_path.h
Outdated
|
||
#ifdef RZ_API | ||
|
||
RZ_API void rz_path_set_prefix(RzPath *sys_path, RZ_NONNULL const char *path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add RZ_NONNULL like for path and check the pointer in the assert (rz_return_if_fail) with the function.
librz/include/rz_util/rz_path.h
Outdated
#ifdef RZ_API | ||
|
||
RZ_API void rz_path_set_prefix(RzPath *sys_path, RZ_NONNULL const char *path); | ||
RZ_API RZ_OWN char *rz_path_prefix(RzPath *sys_path, RZ_NULLABLE const char *path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
librz/include/rz_util/rz_path.h
Outdated
@@ -30,6 +39,11 @@ RZ_API RZ_OWN char *rz_path_home_expand(RZ_NULLABLE const char *path); | |||
|
|||
RZ_API RZ_OWN char *rz_path_realpath(RZ_NULLABLE const char *path); | |||
|
|||
RZ_API RZ_OWN RzPath *rz_path_new(void); | |||
RZ_API void rz_path_free(RzPath *p); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here add RZ_NULLABLE but you don't need to add any assert
@@ -12,28 +12,6 @@ | |||
#include <rz_constructor.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this also
librz/util/path.c
Outdated
* \param path Path to put in the install prefix context or NULL to just get the install prefix | ||
* \return \p path prefixed by the Rizin install prefix or just the install prefix | ||
*/ | ||
RZ_API RZ_OWN char *rz_path_prefix(RZ_NULLABLE const char *path) { | ||
RZ_API RZ_OWN char *rz_path_prefix(RzPath *sys_path, RZ_NULLABLE const char *path) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
librz/util/path.c
Outdated
@@ -135,28 +115,28 @@ RZ_API RZ_OWN char *rz_path_prefix(RZ_NULLABLE const char *path) { | |||
* \brief Return the directory where include files are placed | |||
*/ | |||
RZ_API RZ_OWN char *rz_path_incdir(void) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These functions needs RzPath as argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I am doing iterations in the process, still need to do a lot of work for rz_path_system
also
*/ | ||
RZ_API RZ_OWN char *rz_path_system_rc(void) { | ||
return rz_path_prefix(RZ_GLOBAL_RC); | ||
RZ_API RZ_OWN char *rz_path_system_rc(RzPath *sys_path) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RZ_NONNULL
librz/util/path.c
Outdated
* \brief Deallocate memory associated with a RzPath pointer | ||
* \return NULL | ||
*/ | ||
RZ_API void rz_path_free(RzPath *p) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RZ_NULLABLE
librz/util/path.c
Outdated
* \return NULL | ||
*/ | ||
RZ_API void rz_path_free(RzPath *p) { | ||
if (p) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invert the if.
if (!p) { return; }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, for code readability?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
@@ -192,7 +192,7 @@ int rz_test_main(int argc, const char **argv) { | |||
int ret = 0; | |||
char *cwd = NULL; | |||
RzTestState state = { 0 }; | |||
|
|||
RzPath *sys_path = rz_path_new(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check for null.
@@ -503,7 +503,7 @@ static int __watch_points_cb(void *user); | |||
static int __references_cb(void *user); | |||
static int __help_cb(void *user); | |||
static int __fortune_cb(void *user); | |||
static int __version_cb(void *user); | |||
static int __version_cb(RzPath *sys_path, void *user); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that user is RzCore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would advice to add a test already for the changes made. The more edits you make without tests, the harder it will be to handle the complexity later.
Also, Coverity will be happy :)
Your checklist for this pull request
RZ_API
function and struct this PR changes.RZ_API
).Detailed description
Remove global/static variables in
util/path.c
, so removed the following:portable_prefix
portable_prefix_searched
portable_prefix_mutex
and merged all of them to a struct
RzPathPortable
ininclude/rz_util.h