-
-
Notifications
You must be signed in to change notification settings - Fork 316
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
r.terraflow: Handle memory reallocation errors gracefully #3970
Conversation
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Bear in mind this header file diff --git a/raster/r.terraflow/unionFind.h b/raster/r.terraflow/unionFind.h
index 698dcfc0c9..fdd084b4ae 100644
--- a/raster/r.terraflow/unionFind.h
+++ b/raster/r.terraflow/unionFind.h
@@ -20,10 +20,14 @@
#define __UNION_FIND
#include <assert.h>
-#include <stdlib.h>
-#include <string.h>
+#include <cstdlib>
+#include <cstring>
#include <iostream>
+extern "C" {
+#include <grass/glocale.h>
+}
+
/* initial range guesstimate */
#define UNION_INITIAL_SIZE 2000
@@ -126,13 +130,21 @@ inline void unionFind<T>::makeSet(T x)
if (x >= maxsize) {
/* reallocate parent */
cout << "UnionFind::makeSet: reallocate double " << maxsize << "\n";
- parent = (T *)realloc(parent, 2 * maxsize * sizeof(T));
- assert(parent);
- memset(parent + maxsize, 0, maxsize * sizeof(T));
- /*reallocate rank */
- rank = (T *)realloc(rank, 2 * maxsize * sizeof(T));
- assert(rank);
- memset(rank + maxsize, 0, maxsize * sizeof(T));
+ if (void *new_parent = std::realloc(parent, 2 * maxsize * sizeof(T))) {
+ parent = static_cast<T *>(new_parent);
+ std::memset(parent + maxsize, 0, maxsize * sizeof(T));
+ }
+ else {
+ G_fatal_error(_("Not enough memory for %s"), "parent");
+ }
+ /* reallocate rank */
+ if (void *new_rank = std::realloc(rank, 2 * maxsize * sizeof(T))) {
+ rank = static_cast<T *>(new_rank);
+ std::memset(rank + maxsize, 0, maxsize * sizeof(T));
+ }
+ else {
+ G_fatal_error(_("Not enough memory for %s"), "rank");
+ }
/*update maxsize */
maxsize *= 2;
} Some notes why:
|
With the latest change to Weblate, I do not know how string extraction is done, but at least in our Make file strings from headers are not extracted Line 34 in 559e088
|
Good point, but that is a completely separate issue. |
I made another issue to track string extraction in header files, so this PR can continue |
Doing these changes leads to following issue I think it is treating the file as C code instead of C++ |
Who gives that error? Our compilation or your tool? It's quite possible that you'd have to configure your tool properly |
yeah mostly i think this issue is at my end since i don't have the setup. |
Do you set
It is more informative if you copy and post the whole log text part related to the build error, where we can see exactly what command resulted in the error. |
I'm confused here. Is the issue discussed local or is adding the C calls to C++ header? Generally speaking, while this is C++ header file, there is no reason not to do the normal thing we do elsewhere, i.e., |
My suggested solution #3970 (comment) works just fine. @ShubhamDesai updated this PR with only a part of that. |
Previously i was using a remote machine where i did not had the entire setup for compilation. Also one more doubt I have since I am exploring this grass GIS |
In root you should
You can start GRASS without installing it, in root: |
The changes you suggested I tried and the build was successful. The issue also got fixed. Also with cppcheck version 2.14.2 two more issues of static style casting got highlighted. unionFind.h:78:12: style: C-style pointer casting [cstyleCast] |
Good! The new issue you found is closely related to the initial issue you addressed so this is perfectly appropriate. You should however replace the assert() with a similar solution to handle error as the previous ones. |
Replaced all the assert statements and remove assert.h header as well. |
Co-authored-by: Nicklas Larsson <n_larsson@yahoo.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.
This looks good to me now, thanks!
Co-authored-by: Nicklas Larsson <n_larsson@yahoo.com>
Co-authored-by: Nicklas Larsson <n_larsson@yahoo.com>
Description
This pull request addresses two issues in
r.terraflow/unionFind.h
related to the use ofrealloc
for memory reallocation. The changes ensure that memory is properly handled whenrealloc
fails.Issues
Changes Made
realloc
forparent
andrank
.realloc
succeeded before updating the original pointers.realloc
fails.