Skip to content

Commit dc9d2c6

Browse files
WarrenWeckessersteppi
authored andcommitted
BUG: special: Fix a memory leak in the AMOS function besy(). (#22423)
Use a unique_ptr to manage the heap-allocated array. Closes gh-22314.
1 parent 1d58ff5 commit dc9d2c6

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

include/xsf/amos.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
namespace xsf {
77

8+
//
9+
// Return sf_error equivalents for AMOS ierr values.
10+
// 'ierr' refers to the last parameter of the AMOS functions
11+
// airy(), besh(), besi(), besj(), besk(), besy(), and biry().
12+
//
813
inline sf_error_t ierr_to_sferr(int nz, int ierr) {
9-
/* Return sf_error equivalents for amos ierr values */
10-
1114
if (nz != 0) {
1215
return SF_ERROR_UNDERFLOW;
1316
}
@@ -23,6 +26,8 @@ inline sf_error_t ierr_to_sferr(int nz, int ierr) {
2326
return SF_ERROR_NO_RESULT;
2427
case 5: /* Algorithm termination condition not met */
2528
return SF_ERROR_NO_RESULT;
29+
case 6: /* Memory allocation failed */
30+
return SF_ERROR_MEMORY;
2631
}
2732

2833
return SF_ERROR_OK;

include/xsf/amos/amos.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696

9797
#include <math.h>
9898
#include <complex.h>
99+
#include <memory> // unique_ptr
99100

100101
namespace xsf {
101102
namespace amos {
@@ -2362,6 +2363,7 @@ inline int besy(
23622363
// CANCE BY ARGUMENT REDUCTION
23632364
// IERR=5, ERROR - NO COMPUTATION,
23642365
// ALGORITHM TERMINATION CONDITION NOT MET
2366+
// IERR=6, Memory allocation failed.
23652367
//
23662368
//***LONG DESCRIPTION
23672369
//
@@ -2450,7 +2452,6 @@ inline int besy(
24502452
std::complex<double> c1, c2, hci, st;
24512453
double elim, exr, exi, ey, tay, xx, yy, ascle, rtol, atol, tol, aa, bb, r1m5;
24522454
int i, k, k1, k2, nz, nz1, nz2;
2453-
std::complex<double>* cwrk = new std::complex<double>[n];
24542455

24552456
xx = std::real(z);
24562457
yy = std::imag(z);
@@ -2467,7 +2468,14 @@ inline int besy(
24672468
nz1 = besh(z, fnu, kode, 1, n, cy, ierr);
24682469
if ((*ierr != 0) && (*ierr != 3)) { return 0; }
24692470

2470-
nz2 = besh(z, fnu, kode, 2, n, cwrk, ierr);
2471+
auto cwrk = std::unique_ptr<std::complex<double>[]>
2472+
{new (std::nothrow) std::complex<double>[n]};
2473+
if (cwrk == nullptr) {
2474+
*ierr = 6; // Memory allocation failed.
2475+
return 0;
2476+
}
2477+
2478+
nz2 = besh(z, fnu, kode, 2, n, cwrk.get(), ierr);
24712479
if ((*ierr != 0) && (*ierr != 3)) { return 0; }
24722480

24732481
nz = (nz1 > nz2 ? nz2 : nz1);
@@ -2531,7 +2539,6 @@ inline int besy(
25312539
if ((st == 0.0) && (ey == 0.0)) { nz += 1; }
25322540
}
25332541

2534-
delete [] cwrk;
25352542
return nz;
25362543
}
25372544

0 commit comments

Comments
 (0)