Skip to content

Commit 46baac1

Browse files
committed
fix: Fix five bugs - critical stability and moderate security issues
Fixed three critical division by zero bugs and two moderate bugs: 1. Wall model convergence - zero-check before division (wall_model.cpp:312) 2. Wall model Gamma - epsilon check for exp(1/Gamma) (wall_model.cpp:398) 3. Radiation emissivity - clamp to [0,1] range (CRadP1Solver.cpp:286,360,434) 4. Restart metadata - correct ITER= offset 9→5 (CSolver.cpp:3386) 5. Filename buffer - replace strcpy with strncpy (CPhysicalGeometry.cpp:8255,8569) Improves solver stability, correctness, and security. Signed-off-by: shbhmexe <[email protected]>
1 parent dac92cc commit 46baac1

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

Common/src/geometry/CPhysicalGeometry.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8252,7 +8252,8 @@ void CPhysicalGeometry::SetSensitivity(CConfig* config) {
82528252

82538253
char str_buf[CGNS_STRING_SIZE], fname[100];
82548254
unsigned short iVar;
8255-
strcpy(fname, filename.c_str());
8255+
strncpy(fname, filename.c_str(), sizeof(fname) - 1);
8256+
fname[sizeof(fname) - 1] = '\0'; // Ensure null termination
82568257
int nRestart_Vars = 5, nFields;
82578258
int* Restart_Vars = new int[5];
82588259
passivedouble* Restart_Data = nullptr;
@@ -8565,7 +8566,8 @@ void CPhysicalGeometry::SetSensitivity(CConfig* config) {
85658566
/*--- First, check that this is not a binary restart file. ---*/
85668567

85678568
char fname[100];
8568-
strcpy(fname, filename.c_str());
8569+
strncpy(fname, filename.c_str(), sizeof(fname) - 1);
8570+
fname[sizeof(fname) - 1] = '\0'; // Ensure null termination
85698571
int magic_number;
85708572

85718573
#ifndef HAVE_MPI

Common/src/wall_model.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,12 @@ void CWallModel1DEQ::WallShearStressAndHeatFlux(const su2double tExchange, const
307307
SU2_MPI::Error("Y+ greater than one: Increase the number of points or growth ratio.", CURRENT_FUNCTION);
308308

309309
/* Define a norm
310+
* Check convergence only if previous values are non-zero to avoid division by zero
310311
*/
311-
if (abs(1.0 - tauWall / tauWall_prev) < tol && abs(1.0 - qWall / qWall_prev) < tol) {
312+
bool tau_converged = (tauWall_prev != 0.0) ? (abs(1.0 - tauWall / tauWall_prev) < tol) : false;
313+
bool q_converged = (qWall_prev != 0.0) ? (abs(1.0 - qWall / qWall_prev) < tol) : false;
314+
315+
if (tau_converged && q_converged) {
312316
converged = true;
313317
}
314318
}
@@ -390,9 +394,20 @@ void CWallModelLogLaw::WallShearStressAndHeatFlux(const su2double tExchange, con
390394
const su2double lhs = -((tExchange - TWall) * rho_wall * c_p * u_tau);
391395
const su2double Gamma = -(0.01 * (Pr_lam * pow(y_plus, 4.0)) / (1.0 + 5.0 * y_plus * pow(Pr_lam, 3.0)));
392396
const su2double rhs_1 = Pr_lam * y_plus * exp(Gamma);
397+
398+
/* Protect against division by zero in exp(1/Gamma) when Gamma is very small */
399+
const su2double eps_gamma = 1e-20;
400+
su2double exp_term;
401+
if (abs(Gamma) > eps_gamma) {
402+
exp_term = exp(1.0 / Gamma);
403+
} else {
404+
/* For very small Gamma, cap the exponential to prevent overflow/division by zero */
405+
exp_term = (Gamma > 0) ? exp(1.0 / eps_gamma) : exp(-1.0 / eps_gamma);
406+
}
407+
393408
const su2double rhs_2 =
394409
(2.12 * log(1.0 + y_plus) + pow((3.85 * pow(Pr_lam, (1.0 / 3.0)) - 1.3), 2.0) + 2.12 * log(Pr_lam)) *
395-
exp(1. / Gamma);
410+
exp_term;
396411
qWall = lhs / (rhs_1 + rhs_2);
397412
} else {
398413
qWall = Wall_HeatFlux;

SU2_CFD/src/solvers/CRadP1Solver.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ void CRadP1Solver::BC_Isothermal_Wall(CGeometry *geometry, CSolver **solver_cont
282282
/*--- Get the specified wall emissivity from config ---*/
283283
Wall_Emissivity = config->GetWall_Emissivity(Marker_Tag);
284284

285+
/*--- Clamp emissivity to valid physical range [0,1] to prevent division by zero ---*/
286+
if (Wall_Emissivity < 0.0) Wall_Emissivity = 0.0;
287+
if (Wall_Emissivity > 1.0) Wall_Emissivity = 1.0;
288+
285289
/*--- Compute the constant for the wall theta ---*/
286290
Theta = Wall_Emissivity / (2.0*(2.0 - Wall_Emissivity));
287291

@@ -356,6 +360,10 @@ void CRadP1Solver::BC_Far_Field(CGeometry *geometry, CSolver **solver_container,
356360
/*--- Get the specified wall emissivity from config ---*/
357361
Wall_Emissivity = config->GetWall_Emissivity(Marker_Tag);
358362

363+
/*--- Clamp emissivity to valid physical range [0,1] to prevent division by zero ---*/
364+
if (Wall_Emissivity < 0.0) Wall_Emissivity = 0.0;
365+
if (Wall_Emissivity > 1.0) Wall_Emissivity = 1.0;
366+
359367
/*--- Compute the constant for the wall theta ---*/
360368
Theta = Wall_Emissivity / (2.0*(2.0 - Wall_Emissivity));
361369

@@ -430,6 +438,10 @@ void CRadP1Solver::BC_Marshak(CGeometry *geometry, CSolver **solver_container, C
430438
/*--- Get the specified wall emissivity from config ---*/
431439
Wall_Emissivity = config->GetWall_Emissivity(Marker_Tag);
432440

441+
/*--- Clamp emissivity to valid physical range [0,1] to prevent division by zero ---*/
442+
if (Wall_Emissivity < 0.0) Wall_Emissivity = 0.0;
443+
if (Wall_Emissivity > 1.0) Wall_Emissivity = 1.0;
444+
433445
/*--- Compute the constant for the wall theta ---*/
434446
Theta = Wall_Emissivity / (2.0*(2.0 - Wall_Emissivity));
435447

SU2_CFD/src/solvers/CSolver.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3383,8 +3383,7 @@ void CSolver::Read_SU2_Restart_Metadata(CGeometry *geometry, CConfig *config, bo
33833383

33843384
position = text_line.find ("ITER=",0);
33853385
if (position != string::npos) {
3386-
// TODO: 'ITER=' has 5 chars, not 9!
3387-
text_line.erase (0,9); InnerIter_ = atoi(text_line.c_str());
3386+
text_line.erase (0,5); InnerIter_ = atoi(text_line.c_str()); // 'ITER=' has 5 chars
33883387
}
33893388

33903389
/*--- Angle of attack ---*/

0 commit comments

Comments
 (0)