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

Add PWR079: Avoid undefined behavior due to uninitialized variables #56

Merged
merged 2 commits into from
Dec 19, 2024

Conversation

alvrogd
Copy link
Collaborator

@alvrogd alvrogd commented Dec 16, 2024

Add entry and benchmark for a new check. My results:

$ lscpu
  Model name:             13th Gen Intel(R) Core(TM) i7-13700H

$ gcc --version                            
gcc (Debian 12.2.0-14) 12.2.0

$ gfortran --version                                                 
GNU Fortran (Debian 12.2.0-14) 12.2.0

$ ./run-benchmarks.py --check PWR079
------------------------------------------------------------------
Benchmark                        Time             CPU   Iterations
------------------------------------------------------------------
PWR079 C Example               556 us          556 us         1256
PWR079 C Improved              558 us          558 us         1256
PWR079 Fortran Example         556 us          556 us         1260
PWR079 Fortran Improved        555 us          555 us         1257

This new check would also resolve #39. Nullifying pointers upon creation is suggested as part of the check.

@alvrogd alvrogd self-assigned this Dec 16, 2024
@alvrogd alvrogd requested review from a team, inaki-amatria and igarcia0 and removed request for a team December 16, 2024 10:02
@alvrogd alvrogd marked this pull request as ready for review December 16, 2024 10:02
Copy link
Contributor

@daniel-otero daniel-otero left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 71 to 72
double sum = sum_array(array, 5);
printf("Sum is: %f\n", sum);
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe avoid using sum here to remove ambiguity with the sum of the sum_array function (same in the Fortran example).

Copy link

@igarcia0 igarcia0 Dec 17, 2024

Choose a reason for hiding this comment

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

Agreed.

Suggested change
double sum = sum_array(array, 5);
printf("Sum is: %f\n", sum);
printf("Sum is: %f\n", sum_array(array, 5));

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

to remove ambiguity

Good catch! Done in both the C and Fortran code examples.

Copy link
Contributor

@inaki-amatria inaki-amatria left a comment

Choose a reason for hiding this comment

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

Looks great! Good job

Checks/PWR079/README.md Show resolved Hide resolved
Checks/PWR079/README.md Show resolved Hide resolved
Checks/PWR079/README.md Show resolved Hide resolved
Checks/PWR079/README.md Outdated Show resolved Hide resolved
Checks/PWR079/README.md Outdated Show resolved Hide resolved
Copy link

@igarcia0 igarcia0 left a comment

Choose a reason for hiding this comment

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

Looks good!

// example_array.c
#include <stdio.h>

__attribute__((pure)) double sum_array(double *array, int size) {

Choose a reason for hiding this comment

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

Nit:

Suggested change
__attribute__((pure)) double sum_array(double *array, int size) {
__attribute__((pure)) double sum_array(double *array, size_t size) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I usually opt for int to prevent including a header, but since stdio.h is already used within this code example, fine by me! :)

Comment on lines 71 to 72
double sum = sum_array(array, 5);
printf("Sum is: %f\n", sum);
Copy link

@igarcia0 igarcia0 Dec 17, 2024

Choose a reason for hiding this comment

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

Agreed.

Suggested change
double sum = sum_array(array, 5);
printf("Sum is: %f\n", sum);
printf("Sum is: %f\n", sum_array(array, 5));

@alvrogd alvrogd merged commit f044084 into main Dec 19, 2024
7 checks passed
@alvrogd alvrogd deleted the feature/AddPWR079 branch December 19, 2024 11:44
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

Successfully merging this pull request may close these issues.

[Fortran] nullify pointers before checking associated status
4 participants