Skip to content
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.stats: Use long total_count to avoid int overflow #3203

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion raster/r.stats/stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ struct Node {

static struct Node **hashtable;
static struct Node *node_list = NULL;
static int node_count = 0, total_count = 0;
static int node_count = 0;
static long total_count = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes (Windows?), int might be just as large as long. While the minimums are 16 and 32, sizes can be 32 and 32 or 32 and 64 according to https://en.cppreference.com/w/cpp/language/types (C++, C does not have that nice description but since it depends on the compiler anyway, I guess it applies the same way).

I think someone brought this up in the past, but I don't recall if we have a best practice for that.

Copy link
Contributor

@nilason nilason Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If total_count is guaranteed to be >=0 unsigned int could be a better alternative, otherwise grass_int64 may be considered.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose long because Node.count is long.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In r.in.pdal, I used typedef which goes for unsigned long or unsigned long long for number of points depending on HAVE_LONG_LONG_INT. However, I think grass_int64 is better as it is aligned with the new(ish) trend of fixed with integers (C99, C++11). Signed versus unsigned and int64_t versus grass_int64, that's whole another discussion.

However, if int->long helps in your case and it fits with the other numbers there, I'm not against merging this as is.


int initialize_cell_stats(int n)
{
Expand Down
Loading