Skip to content

Fix aliasing violation and out-of-bounds write in lo_permutations - #174

Merged
mjv500 merged 2 commits into
tdep-developers:mainfrom
SergeevGregory:main
Aug 24, 2026
Merged

Fix aliasing violation and out-of-bounds write in lo_permutations#174
mjv500 merged 2 commits into
tdep-developers:mainfrom
SergeevGregory:main

Conversation

@SergeevGregory

Copy link
Copy Markdown
Contributor

Description

This PR fixes a critical Fortran standard violation (aliasing) in gottochblandat_sorting.f90 that causes silent heap corruption and subsequent Segmentation fault (SIGSEGV) when compiling with modern compilers (like gfortran 9.3+) under -O2 or -O3 optimization.

The Bug

In the lo_permutations subroutine, the recursive generate function takes the loop counter as a dummy argument k. However, internally it was writing to the array using the outer variable i:
pm(:,i) = permutation

Under -O2 optimization, register caching causes this to repeatedly write to pm(:,0) (an out-of-bounds write), which silently overwrites the heap descriptors and crashes the program later during deallocation.

The Fix

Changed the array index to use the correct local dummy argument:
pm(:,k) = permutation

This ensures strict compliance with the Fortran standard and completely resolves the memory corruption issue on optimized builds.

@mjv500 mjv500 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Had also found this on some architectures. Thanks Gregory

@mjv500
mjv500 merged commit 7976bfc into tdep-developers:main Aug 24, 2026
1 check passed
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.

2 participants