GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix the JOBR = 'T' path of xGESVDQ: wrong TRANS in the real routines, invalid read in all four by rmlarsen · Pull Request #1513 · Reference-LAPACK/lapack

Conversation

@rmlarsen

@rmlarsen rmlarsen commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

AI disclosure: This PR was prepared with Claude Code.

Summary

The path of xGESVDQ that handles JOBR = 'T' with all right singular vectors (JOBV = 'A') and a rank-deficient matrix has two defects. Two commits, one for each.

  1. DGESVDQ and SGESVDQ call DORMQR/SORMQR with TRANS = 'C', which the real routines reject (they accept 'N' or 'T'). The reference XERBLA stops the program. DGESVDQ('A','N','T','A','A',8,6,...) with LWORK = -1 stops with "On entry to DORMQR parameter number 2 had an illegal value", and so does a solve on an 8-by-6 matrix of rank 1.
  2. In all four precisions the same path copies a triangular factor with a loop bound N instead of NR, which reads past the end of U. It is an invalid read that does not change any result. On master it is reachable only in the complex routines, because the real ones stop earlier.

Description

Both defects are in xGESVDQ in the branch for JOBR = 'T', JOBV = 'A' and a left-vector option other than 'N', when R has been truncated to the numerical rank NR with 2*NR <= N. Truncation needs JOBA = 'A' or 'M'; with JOBA = 'H' or 'E', NR = N and the branch is not taken. The test driver only called xGESVDQ('H','N','N','A','A'), so neither the 'T' path nor the workspace query was ever run.

  1. The calls are the workspace query (reached when LWORK = -1, JOBR = 'T', JOBV = 'A' and JOBU is not 'N'; a real xORMQR query, whose argument check fails first) and the computation of V in this branch. The complex twins call xUNMQR, for which 'C' is valid, and the real code was evidently transcribed from them. xGESVDQ does not check the INFO of its inner xORMQR query, so a wrong TRANS there is silent when XERBLA returns, as the test suite's does.
  2. After xGEQRF( N, NR, U(1,NR+1), ... ) the triangle is in U(1:NR,NR+1:2*NR). The copy into V runs DO 1143 p = 1, NR / DO 1144 q = 1, N / V(q,p) = U(p,NR+q), so it reads U(1:NR,NR+1:NR+N). For JOBU = 'S', 'R', 'F' or 'U', U has N columns, so columns N+1 to NR+N do not exist; for JOBU = 'A' they do not whenever NR+N > M. The extra values go to V(NR+1:N,1:NR), which xGESVD does not use and xLASET overwrites. The non-transposed twin copies exactly the triangle, with xLACPY( 'L', NR, NR, ... ).

I went through the rest of this path once more (the copy of R**T into U, xGEQRF, xGESVD on the NR-by-NR factor, the xORMQR/xUNMQR application, xLAPMT, and the assembly of U, with the workspace offsets and minimum workspace sizes) and found no other slip. The result is a correct SVD, below.

Fix

  1. Use 'T' in the four real calls (two each in dgesvdq.f and sgesvdq.f).
  2. Bound the inner loop by NR in dgesvdq.f, sgesvdq.f, cgesvdq.f and zgesvdq.f; CONJG stays in the complex routines.

Nothing else changes; the values in V(NR+1:N,1:NR) were overwritten before being used.

Reproducer

For commit 1, run on master and with the fix (a rank-1 8-by-6 matrix, JOBA = 'A', JOBR = 'T', JOBU = JOBV = 'A'):

program repro
   implicit none
   integer, parameter :: m = 8, n = 6
   double precision :: a(m,n), a0(m,n), s(n), u(m,m), vt(n,n), x(m), y(n)
   double precision :: work(1000), rwork(10), q(2), res, ou, ov
   integer :: iwork(100), info, numrank, i, j, iq(2)
   do i = 1, m; x(i) = 1d0 + 0.1d0*i; end do
   do j = 1, n; y(j) = 1d0 - 0.1d0*j; end do
   do j = 1, n; a(:,j) = x*y(j); end do          ! rank 1
   a0 = a
   ! workspace query: JOBA='A', JOBR='T', JOBU='A', JOBV='A'
   call dgesvdq('A','N','T','A','A',m,n,a,m,s,u,m,vt,n,numrank,iq,-1,q,-1,rwork,-1,info)
   print '(a,i3,a,2f8.1)', 'query: info =', info, ', lwork =', q
   call dgesvdq('A','N','T','A','A',m,n,a,m,s,u,m,vt,n,numrank,iwork,100,work,1000,rwork,10,info)
   print '(a,i3,a,i3)', 'solve: info =', info, ', numrank =', numrank
   do j = 1, n; do i = 1, m
      a0(i,j) = a0(i,j) - sum(u(i,1:n)*s*vt(1:n,j))
   end do; end do
   res = maxval(abs(a0))
   ou = 0; ov = 0
   do i = 1, m; do j = 1, m
      ou = max(ou, abs(dot_product(u(:,i),u(:,j)) - merge(1d0,0d0,i==j)))
   end do; end do
   do i = 1, n; do j = 1, n
      ov = max(ov, abs(dot_product(vt(i,:),vt(j,:)) - merge(1d0,0d0,i==j)))
   end do; end do
   print '(a,3es10.2)', 'max|A-U S VT|, |U^TU-I|, |VT VT^T-I| =', res, ou, ov
end program

On master the program stops at the query (and in the solve, if the query call is removed):

 ** On entry to DORMQR parameter number  2 had an illegal value

With commit 1:

query: info =  0, lwork =  2342.0    36.0
solve: info =  0, numrank =  1
max|A-U S VT|, |U^TU-I|, |VT VT^T-I| =  4.44E-16  6.66E-16  2.22E-16

For commit 2, a second program (repro2.f90; it calls DGESVDQ with JOBU = 'S' on the same matrix, U allocated 8-by-6, and the queried minimal workspace) is built with -g -fsanitize=address, once with commit 1 only and once with both commits. With commit 1 only, AddressSanitizer reports heap-buffer-overflow ... READ of size 8 in DGESVDQ, at the copy loop. With both commits it prints info = 0, numrank = 1, s1 = 6.84E+00 and no error. Valgrind reports the same read (Invalid read of size 8 ... 0 bytes after a block of size 768) for ZGESVDQ on a complex matrix of rank 1 with JOBU = 'S', and none with the fix. It also reports it for JOBU = 'A' on a 6-by-6 matrix, where NR+N > M. The singular values are the same before and after.

Regression test

Commit 1. xDRVBD (real) now also calls xGESVDQ('A','N','T','A','A') on a copy of the test matrix whose first N/2 columns are repeated to fill it, so its rank is at most N/2. The call is a workspace query first. The test requires consistent values (minimal LIWORK, LWORK and LRWORK, optimal at least minimal) and that no routine called XERBLA during the query, using the LERR flag of the test XERBLA. The solve then gets the queried minimal LWORK for IWS = 1, the queried optimal LWORK (or all of LWORK, if less) for IWS = 4, and lengths in between, followed by a guard region of at most 8 entries. Tests 40-43 check the residual |A - U diag(S) VT|, orthogonality of U and VT, and the ordering of S together with the guard, the XERBLA flag and the query values; a NaN fails. The block runs last in the loop, because it overwrites USAV, VTSAV and SSAV, and placed before the xGESVJ and xGEJSV tests it made their tests 16, 20 and 24 fail for the overflow matrix type.

On the parent commit 236 to 248 of the 16340 DBD and SBD driver tests fail per run (tests 40 and 43), with XERBLA was called with SRNAME = DORMQR instead of DGESVD (SORMQR for single). With commit 1, all 16340 pass in each precision. Putting 'C' back in the query call of DGESVDQ alone, with the computation call correct, fails 140 tests (all test 43), so the query check is effective.

Commit 2. The suite cannot detect an invalid read, so the validation is the program above. CDRVBD and ZDRVBD get the same test, as a check that this path computes a correct SVD in the complex precisions; it passes on the parent as well, and so does not show the fix.

Test suite

The full test suite passes, configured as in CI (CBLAS=ON, LAPACKE=ON, LAPACKE_WITH_TMG=ON) with the _64 API. All 333 CTest entries pass, with 5775988 LAPACK tests (5775990 for the _64 API), 11973112 LAPACKE tests, 319677 BLAS tests and 645492 CBLAS tests for each API, and 0 numerical errors and 0 other errors. That is 20800 LAPACK tests more per API than on master, the tests added here. A second build with CI's gfortran flags (-O2 -Wall -Wno-unused-dummy-argument -Wno-unused-variable -Wno-unused-label -Werror=conversion -fimplicit-none -frecursive -fcheck=all) passes the suite as well, and the changed files compile with the same warnings as on master. The builds used GNU Fortran and GCC 15.2.0 with CMAKE_BUILD_TYPE=Release on x86-64 Linux.

Checklist

  • The documentation has been updated. (Not needed: the routines now do what they document.)
  • If the PR solves a specific issue, it is set to be closed on merge. (No tracking issue.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
rmlarsen and others added 2 commits November 21, 2026 19:50
@rmlarsen @claude
Call xORMQR with TRANS = 'T' in DGESVDQ and SGESVDQ
a6251d0
DGESVDQ and SGESVDQ call DORMQR/SORMQR with TRANS = 'C' in two places:
the workspace query for JOBR = 'T' with all of U and V requested, and
the computation that assembles V when JOBR = 'T', JOBV = 'A', R has been
truncated to the numerical rank NR and 2*NR <= N.  The real xORMQR
accepts only 'N' or 'T', so it reports argument 2 through XERBLA, and
the reference XERBLA stops the program.  The complex routines call
xUNMQR, for which 'C' is valid, and the real code was evidently
transcribed from them.

Both paths were therefore never executable.  DGESVDQ('A','N','T','A',
'A',8,6,...) with LWORK = -1 stops with "On entry to DORMQR parameter
number 2 had an illegal value", and so does a solve on an 8-by-6 matrix
of rank 1, where JOBA = 'A' truncates R to NR = 1.  With JOBA = 'H' or
'E' no truncation is attempted, so NR = N and the second call is never
reached.

Use 'T', the real equivalent of the conjugate transpose.  With it both
paths run and give a correct SVD: for the rank-1 8-by-6 matrix the
residual max|A - U S VT| is 4e-16 and |U**T U - I|, |VT VT**T - I| are
7e-16 and 2e-16.  Other inputs behave exactly as before.

Test: DDRVBD and SDRVBD now also call xGESVDQ with JOBA = 'A' and
JOBR = 'T' on a matrix whose first N/2 columns are repeated to fill it,
so that its rank is at most N/2.  The call is first a workspace query,
whose result must be consistent and must not make any routine call
XERBLA (xGESVDQ does not check the INFO of its inner xORMQR query, so
this is detected through the LERR flag of the test XERBLA).  The solve
then gets the queried minimal LWORK in one IWS iteration and the queried
optimal LWORK in another, with a guard region after the workspace.
Tests 40-43 check the residual, the orthogonality of U and VT, and the
ordering of S, the guard, the flags and the query values.  The old
driver only called xGESVDQ('H','N','N','A','A'), which never takes
either path.  On the parent commit 236 to 248 of the 16340 DBD (SBD)
driver tests fail per run, tests 40 and 43, with XERBLA reporting DORMQR
(SORMQR) parameter 2; with this change all 16340 pass in both
precisions.  Restoring 'C' in the query call of DGESVDQ alone fails 140 tests (43).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@rmlarsen @claude
Read only the NR-by-NR triangle in the JOBR = 'T' path of xGESVDQ
59bb530
When JOBR = 'T', JOBV = 'A', R has been truncated to the numerical rank
NR and 2*NR <= N, xGESVDQ computes a QR factorization of the N-by-NR
matrix holding R**T (or R**H) in U(1:N,NR+1:2*NR), and copies the
triangular factor, which occupies U(1:NR,NR+1:2*NR), into V with

   DO 1143 p = 1, NR
      DO 1144 q = 1, N
         V(q,p) = U(p,NR+q)

The triangle is NR-by-NR, but q runs to N, so the loop also reads
U(1:NR,2*NR+1:NR+N).  For JOBU = 'S', 'R', 'F' or 'U', U has N columns,
and columns N+1 to NR+N do not exist; for JOBU = 'A' they do not exist
whenever NR+N > M.  The values read are copied into V(NR+1:N,1:NR),
which xGESVD does not use and xLASET overwrites afterwards, so results
are unaffected, but it is an invalid read.  Valgrind reports, for ZGESVDQ on a rank-1 8-by-6 matrix with JOBU = 'S'
and U allocated 8-by-6, "Invalid read of size 8 ... Address is 0 bytes
after a block of size 768", and AddressSanitizer reports a
heap-buffer-overflow READ in DGESVDQ for the same case.  The
complex routines reach this path on master; the real routines can reach
it since the previous commit.

Bound the inner loop by NR, in all four precisions (CONJG is kept in the
complex ones).  Nothing else in this path reads or writes outside the
arrays: the neighboring loops over R**T and the transposition loops
already stop at NR or N as their matrices require, and the non-
transposed twin copies the triangle with xLACPY( 'L', NR, NR, ... ).

Results are unchanged; V(NR+1:N,1:NR) held values that were overwritten
before use.

Test: the suite cannot detect an invalid read, so the fix was checked
with AddressSanitizer (DGESVDQ) and valgrind (ZGESVDQ) on rank-1 6-by-6
and 8-by-6 matrices with JOBU = 'A' and 'S', U allocated with exactly
the size the routine documents: errors on the parent, none with this
change, and identical singular values.  CDRVBD and ZDRVBD now also run
xGESVDQ with JOBA = 'A', JOBR = 'T' on a matrix of rank at most N/2,
as DDRVBD and SDRVBD do, with a workspace query, the queried minimal
and optimal LWORK and a guard region; they pass on the parent and with
this change, since they only show that the path computes a correct SVD.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@codecov

codecov Bot commented Nov 22, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 60 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.76%. Comparing base (ef55c9b) to head (40704ff).
⚠️ Report is 3 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
TESTING/EIG/cdrvbd.f 77.61% 15 Missing ⚠️
TESTING/EIG/ddrvbd.f 77.27% 15 Missing ⚠️
TESTING/EIG/sdrvbd.f 77.27% 15 Missing ⚠️
TESTING/EIG/zdrvbd.f 77.61% 15 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1513      +/-   ##
==========================================
+ Coverage   70.63%   70.76%   +0.12%     
==========================================
  Files        6243     6243              
  Lines      501290   501652     +362     
  Branches    23516    23516              
==========================================
+ Hits       354100   355006     +906     
+ Misses     146327   145783     -544     
  Partials      863      863              
Components Coverage Δ
BLAS 99.73% <ø> (ø)
CBLAS 96.95% <ø> (ø)
LAPACK 84.04% <100.00%> (+0.30%) ⬆️
LAPACKE 4.18% <ø> (ø)
TMGLIB 55.69% <ø> (ø)
BLAS testing 88.79% <ø> (ø)
CBLAS testing 89.65% <ø> (ø)
LAPACK testing 82.40% <77.44%> (-0.01%) ⬇️
LAPACKE testing ∅ <ø> (∅)
Files with missing lines Coverage Δ
SRC/cgesvdq.f 61.22% <100.00%> (+24.30%) ⬆️
SRC/dgesvdq.f 61.82% <100.00%> (+23.76%) ⬆️
SRC/sgesvdq.f 61.82% <100.00%> (+23.76%) ⬆️
SRC/zgesvdq.f 61.22% <100.00%> (+24.30%) ⬆️
TESTING/EIG/cdrvbd.f 81.95% <77.61%> (-0.69%) ⬇️
TESTING/EIG/ddrvbd.f 81.36% <77.27%> (-0.67%) ⬇️
TESTING/EIG/sdrvbd.f 81.36% <77.27%> (-0.67%) ⬇️
TESTING/EIG/zdrvbd.f 81.95% <77.61%> (-0.69%) ⬇️

... and 20 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ef55c9b...40704ff. Read the comment docs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

rmlarsen
rmlarsen commented Nov 24, 2026
View reviewed changes
Comment thread TESTING/EIG/zdrvbd.f Outdated
$ LSWORK = LOPT
LG = MIN( 8, LWORK-LSWORK )
DO 198 J = LSWORK + 1, LSWORK + LG
WORK( J ) = DCMPLX( GUARD, GUARD )

@rmlarsen rmlarsen Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

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.

DCMPLX is a non-standard intrinsic. 773fd2f ("TESTING: replace non-standard double complex intrinsics") removed every use from TESTING because NAG Fortran only accepts it under -dcfuns, and this reintroduces it (here and line 1340). The NAG jobs are skipped on fork PRs (they finish in a few seconds here) but do build on master, so this would only show up after merge. Use CMPLX( ..., KIND=... ) with a kind parameter, as that commit did.

Found in an AI-assisted self-review (Claude Code).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@rmlarsen rmlarsen Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

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.

Fixed in 40704ff: both uses are now CMPLX( ..., KIND=DP ).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@rmlarsen @claude
TESTING: use the standard CMPLX in the new ZDRVBD test
40704ff
The test added on this branch builds its double complex values with
DCMPLX, a VAX extension that NAG Fortran only accepts under -dcfuns.
773fd2f removed every such use from TESTING, and the NAG jobs do not
run on pull requests from forks, so the build would only break after
the merge.

Use CMPLX( ..., KIND=DP ) with a DP kind parameter, as that commit
did. The values are the same; the complex16 LAPACK tests of this
branch run 1154029 tests with no failures.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@rmlarsen
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.