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

Warnings in ArchLinux (GCC 10?) #42

Closed
hv15 opened this issue Dec 7, 2020 · 1 comment
Closed

Warnings in ArchLinux (GCC 10?) #42

hv15 opened this issue Dec 7, 2020 · 1 comment

Comments

@hv15
Copy link
Member

hv15 commented Dec 7, 2020

When building the stdlib on ArchLinux (and presumably any system using GCC 10), we get the following warnings:

/builds/gitlab/sac-group/sac2c/build/sac2c-release/src/sac2c-release-build/include/runtime/phm_h/phm.h:68:23: warning: argument 1 range [18446744065119617024, 18446744073709551612] exceeds maximum object size 9223372036854775807 [-Walloc-size-larger-than=]
   68 | #define SAC_MALLOC(x) malloc (x)
      |                       ^~~~~~~~~~
/builds/gitlab/sac-group/sac2c/stdlib/src/stdio/src/ArrayIO/PrintArray.c:203:20: note: in expansion of macro ‘SAC_MALLOC’
  203 |       index=(int *)SAC_MALLOC(sizeof(int)*dim);
      |                    ^~~~~~~~~~
In file included from /builds/gitlab/sac-group/sac2c/stdlib/src/stdio/src/ArrayIO/PrintArray.c:5:
/builds/gitlab/sac-group/sac2c/stdlib/src/stdio/src/ArrayIO/PrintArray.c: In function ‘ARRAYIO__PrintCharArrayFormat’:
/usr/include/stdlib.h:539:14: note: in a call to allocation function ‘malloc’ declared here
  539 | extern void *malloc (size_t __size) __THROW __attribute_malloc__
      |              ^~~~~~
/builds/gitlab/sac-group/sac2c/stdlib/src/stdio/src/ArrayIO/PrintArray.c: At top level:

These warning seem to indicate that the input for malloc may exceed some upper limit, leading to invalid allocation.

A quick look at GCC bug-report 85783 shows that this warning happens in cases where are compile-time can't determine if the input to malloc is sane. A suggested fix is to re-cast the input to something compatible with size_t, e.g. unsigned int.

@hv15
Copy link
Member Author

hv15 commented Dec 7, 2020

Ok, looking at the code in src/stdio/src/ArrayIO/PrintArray.c, we have the following lines (refactored to makes things a bit more clear):

void PrintArr(FILE *stream, int typeflag, string format, int dim, int * shp, void *a) {
...
  if (dim == 0) {
    // we do some stuff here
  } else {
    index = malloc (dim * sizeof (int));
    // we do something with `index`
  }
}

If I use a cast to unsigned int in malloc, e.g. malloc (((unsigned int)(dim)) * sizeof (int)), the warnings go away. I don't feel comfortable doing this as its a bit of kludge.

The source of the problem as far as I can see is that because we are calling PrintArr from a stdlib function (printarray), the C compiler can only infer the range of the values for dim based on its type. By casting to unsigned int we limit this range to positive values which is less than PTRDIFF_MAX, so there is no warning any more. Note that the value of PTRDIFF_MAX is set as the default for -Walloc-size-larger-than= to check against.

An alternative solution is to perform some kind of check for dim > 0.

hv15 added a commit to hv15/Stdlib that referenced this issue Dec 7, 2020
hv15 added a commit to hv15/Stdlib that referenced this issue Dec 7, 2020
hv15 added a commit to hv15/Stdlib that referenced this issue Dec 8, 2020
@hv15 hv15 closed this as completed Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant