-
-
Notifications
You must be signed in to change notification settings - Fork 301
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
lib/gis: Add portable G_strlcpy function #4101
Conversation
@ShubhamDesai @echoix everywhere a |
|
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 this is a great addition, after just reading the code I have some suggestions.
How does windows support look like? (Passing CI doesn't mean anything for now) |
|
|
Like for Linux, Windows doesn't have |
Yes, it is not required, but a compiler optimisation which we now can use (since we are supporting C11 standard). I don't think it is necessary to declare it in the prototype (gis.h). |
a2088a1
to
635f8a0
Compare
In fact, I think, |
|
An alternative way, could possibly be to use: extern "C"
{
#include "grass/gis.h"
} in all C++ files. Like: grass/imagery/i.atcorr/main.cpp Lines 41 to 46 in 0645510
Or add a: #ifdef __cplusplus
extern "C"
{
#endif
// original content of gis.h
#ifdef __cplusplus
}
#endif then we can probably use The former (the extern "C" version for cpp files), if this solves the problem with the restrict keyword, is probably the better solution anyway. |
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.
With the following suggested changes also C++ file can include gis.h. 'export "C"' doesn't to the trick.
(Historically, prototypes in GRASS doesn't have named parameters.)
Nice, thanks! |
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'm ready to merge this if there are no objections.
I have a very minor stylistic suggestion below. And since we have a squash-and-merge policy, I would prefer if you'd remove the r.path
changes, to keep clean commits (it may be addressed with #4087).
This commit introduces a new G_strlcpy function in lib/gis, inspired by G_asprintf. G_strlcpy provides a safer alternative to strcpy and strncpy, with consistent behavior across different systems. Key points: - Implements strlcpy functionality, available natively on BSD systems - Portable implementation for non-BSD systems (excluding Linux with libbsd) - Based on FreeBSD's implementation: https://github.com/freebsd/freebsd-src/blob/98dd639c94f716858ae29958f484729b1d2fd387/sys/libkern/strlcpy.c#L28 - Designed to replace unsafe uses of strcpy and strncpy throughout the project The function is implemented to use the native strlcpy where available, falling back to our portable version on systems without it. This ensures optimal performance on BSD systems while maintaining compatibility across different platforms. By providing G_strlcpy, we aim to improve the overall safety and consistency of string operations in our codebase.
…files to include gis.h without 'extern "C"' trick. Update function prototypes to use named parameters. Co-authored-by: Nicklas Larsson <n_larsson@yahoo.com>
Co-authored-by: Nicklas Larsson <n_larsson@yahoo.com>
Thanks. I removed the |
@lbartoletti Thanks!! |
This commit introduces a new G_strlcpy function in lib/gis, inspired by G_asprintf. G_strlcpy provides a safer alternative to strcpy and strncpy, with consistent behavior across different systems. Key points: - Implements strlcpy functionality, available natively on BSD systems - Portable implementation for non-BSD systems (excluding Linux with libbsd) - Based on FreeBSD's implementation: https://github.com/freebsd/freebsd-src/blob/98dd639c94f716858ae29958f484729b1d2fd387/sys/libkern/strlcpy.c#L28 - Designed to replace unsafe uses of strcpy and strncpy throughout the project The function is implemented to use the native strlcpy where available, falling back to our portable version on systems without it. This ensures optimal performance on BSD systems while maintaining compatibility across different platforms. By providing G_strlcpy, we aim to improve the overall safety and consistency of string operations in our codebase.
From commit message:
Marked as draft for now, since only tested on FreeBSD, which have strlcpy of course 😉
Supersed #4087 for the "test"