From 8f63689fcad621703d2c69de2a19c2dd7ac5927f Mon Sep 17 00:00:00 2001 From: DanielZhao0432 Date: Sat, 21 Nov 2026 21:08:26 +0800 Subject: [PATCH 1/7] Fix(cell): require LATTICE_PARAMETERS for non-cubic named lattices Confirmed empirically on current develop: a named non-cubic lattice without LATTICE_PARAMETERS silently builds a zero-volume cell (hexagonal, trigonal, ...: shape factors stay zero) or a NaN cell (triclinic: 0/0 in the shape formula), and the reciprocal-lattice inversion then fails far from the actual input mistake. - read_lattice_constant(): require the LATTICE_PARAMETERS line for all non-cubic named lattices and quit with a clear message naming the lattice type otherwise; - reject degenerate (zero-volume) or non-finite lattice vectors after construction, covering malformed parameter values and the LATTICE_VECTORS path as well. Refs #8354. --- source/source_cell/read_atom_species.cpp | 23 ++++ source/source_cell/unittests/CMakeLists.txt | 7 + .../unittests/test_read_atom_species.cpp | 127 ++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 source/source_cell/unittests/test_read_atom_species.cpp diff --git a/source/source_cell/read_atom_species.cpp b/source/source_cell/read_atom_species.cpp index 1dc1afb75d3..7d9d8ee7539 100644 --- a/source/source_cell/read_atom_species.cpp +++ b/source/source_cell/read_atom_species.cpp @@ -1,5 +1,6 @@ #include "read_stru.h" +#include #include #include "source_base/tool_title.h" @@ -198,6 +199,19 @@ bool read_lattice_constant(std::ifstream& ifa, ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", "do not use LATTICE_VECTORS along with explicit specification of lattice type"); } + // Non-cubic named lattices cannot be constructed without the shape + // parameters in LATTICE_PARAMETERS; without them the shape variables + // stay zero and produce a zero-volume or NaN cell. + if(latName != "sc" && latName != "fcc" && latName != "bcc") + { + const bool has_params = + ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS", true, false); + if (!has_params) + { + const std::string msg = "LATTICE_PARAMETERS is required for lattice type " + latName; + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", msg); + } + } if(latName=="sc") {//simple-cubic, ibrav = 1 latvec.e11 = 1.0; latvec.e12 = 0.0; latvec.e13 = 0.0; @@ -379,6 +393,15 @@ bool read_lattice_constant(std::ifstream& ifa, } } + // Reject degenerate (zero-volume) or non-finite lattice vectors, whether + // they came from LATTICE_VECTORS or from a named lattice. + const double latvec_det = latvec.Det(); + if (!std::isfinite(latvec_det) || latvec_det == 0.0) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "the lattice vectors form a zero-volume or non-finite cell"); + } + // lattice vectors in another form. lat.a1.x = latvec.e11; lat.a1.y = latvec.e12; diff --git a/source/source_cell/unittests/CMakeLists.txt b/source/source_cell/unittests/CMakeLists.txt index 0a8d0f15c9e..fc4782d50e2 100644 --- a/source/source_cell/unittests/CMakeLists.txt +++ b/source/source_cell/unittests/CMakeLists.txt @@ -33,3 +33,10 @@ addtest( TARGET MODULE_CELL_index_test LIBS base device SOURCES test_cell_index.cpp ../cell_index.cpp) + +addtest( + TARGET MODULE_CELL_read_lattice_validation_test + LIBS base device + SOURCES test_read_atom_species.cpp + ../read_atom_species.cpp + ../basecell.cpp) diff --git a/source/source_cell/unittests/test_read_atom_species.cpp b/source/source_cell/unittests/test_read_atom_species.cpp new file mode 100644 index 00000000000..d92a1a55c4c --- /dev/null +++ b/source/source_cell/unittests/test_read_atom_species.cpp @@ -0,0 +1,127 @@ +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include +#include +#include + +#include "source_cell/basecell.h" +#include "source_cell/read_stru.h" + +/************************************************ + * unit test of read_atom_species.cpp lattice validation + ***********************************************/ + +/** + * - Tested Functions: + * - HexagonalMissingParamsRejected / TrigonalMissingParamsRejected + * - read_lattice_constant(): non-cubic named lattices require + * LATTICE_PARAMETERS; without it the shape variables stayed zero and + * produced a zero-volume or NaN cell (issue #8354) + * - ValidHexagonalWithParams + * - a hexagonal lattice with c/a given still reads normally + * - CubicScNeedsNoParams + * - cubic lattice names do not need LATTICE_PARAMETERS + * - DegenerateLatticeVectorsRejected + * - explicit LATTICE_VECTORS forming a zero-volume cell are rejected + */ + +class ReadLatticeValidationTest : public ::testing::Test +{ + protected: + Lattice lat; + std::ofstream ofs_running; + std::string log_path; + std::string stru_path; + + void SetUp() override + { + log_path = testing::TempDir() + "read_lattice_validation.log"; + ofs_running.open(log_path); + } + + void TearDown() override + { + ofs_running.close(); + std::remove(log_path.c_str()); + if (!stru_path.empty()) + { + std::remove(stru_path.c_str()); + } + } + + std::string write_stru_file(const std::string& name, const std::string& content) + { + stru_path = testing::TempDir() + name; + std::ofstream ofs(stru_path); + ofs << content; + ofs.close(); + ofs_running << "created test STRU file: " << stru_path << std::endl; + return stru_path; + } +}; + +TEST_F(ReadLatticeValidationTest, HexagonalMissingParamsRejected) +{ + const std::string stru = write_stru_file("hex_no_param.stru", "LATTICE_CONSTANT\n4.0\n"); + lat.latName = "hexagonal"; + std::ifstream ifa(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("LATTICE_PARAMETERS is required for lattice type hexagonal")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, TrigonalMissingParamsRejected) +{ + const std::string stru = write_stru_file("trigonal_no_param.stru", "LATTICE_CONSTANT\n4.0\n"); + lat.latName = "trigonal"; + std::ifstream ifa(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("LATTICE_PARAMETERS is required for lattice type trigonal")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, ValidHexagonalWithParams) +{ + const std::string stru = + write_stru_file("hex_with_param.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n1.633\n"); + lat.latName = "hexagonal"; + std::ifstream ifa(stru); + EXPECT_TRUE(unitcell::read_lattice_constant(ifa, ofs_running, lat)); + EXPECT_DOUBLE_EQ(lat.latvec.e33, 1.633); + EXPECT_GT(lat.latvec.Det(), 0.0); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, CubicScNeedsNoParams) +{ + const std::string stru = write_stru_file("sc_no_param.stru", "LATTICE_CONSTANT\n4.0\n"); + lat.latName = "sc"; + std::ifstream ifa(stru); + EXPECT_TRUE(unitcell::read_lattice_constant(ifa, ofs_running, lat)); + EXPECT_DOUBLE_EQ(lat.latvec.Det(), 1.0); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, DegenerateLatticeVectorsRejected) +{ + const std::string stru = write_stru_file("degenerate.stru", + "LATTICE_CONSTANT\n4.0\n" + "LATTICE_VECTORS\n" + "1 0 0\n" + "0 1 0\n" + "0 0 0\n"); + lat.latName = "user_defined_lattice"; + std::ifstream ifa(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), + ::testing::ExitedWithCode(1), + ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("zero-volume")); + ifa.close(); +} From 8d83960575318b06ff35df6e163e7d270ac84b5a Mon Sep 17 00:00:00 2001 From: DanielZhao0432 Date: Sat, 21 Nov 2026 22:36:39 +0800 Subject: [PATCH 2/7] Fix(cell): validate trigonal cos(alpha) extraction in LATTICE_PARAMETERS A failed extraction left cosab = 0 and silently built the simple-cubic (alpha = 90 degree) special case instead of the intended trigonal cell; this is the only named-lattice branch where a malformed parameter line still yields a finite, nonzero-volume cell, so the determinant check alone does not catch it. Also add the corresponding death test and run the new test binary locally: 6/6 pass. --- source/source_cell/read_atom_species.cpp | 11 ++++++++++- .../unittests/test_read_atom_species.cpp | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/source/source_cell/read_atom_species.cpp b/source/source_cell/read_atom_species.cpp index 7d9d8ee7539..ffe6320575d 100644 --- a/source/source_cell/read_atom_species.cpp +++ b/source/source_cell/read_atom_species.cpp @@ -248,7 +248,16 @@ bool read_lattice_constant(std::ifstream& ifa, if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { double cosab=0.0; - ModuleBase::GlobalFunc::READ_VALUE(ifa, cosab); + // READ_VALUE would hide an extraction failure here: a failed + // read leaves cosab = 0, silently building the simple-cubic + // (alpha = 90 degree) special case instead of the intended + // trigonal cell. + ifa >> cosab; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid cos(alpha) in LATTICE_PARAMETERS for lattice type trigonal"); + } t1 = sqrt(1.0 + 2.0*cosab); t2 = sqrt(1.0 - cosab); } diff --git a/source/source_cell/unittests/test_read_atom_species.cpp b/source/source_cell/unittests/test_read_atom_species.cpp index d92a1a55c4c..c6f82fc1070 100644 --- a/source/source_cell/unittests/test_read_atom_species.cpp +++ b/source/source_cell/unittests/test_read_atom_species.cpp @@ -18,6 +18,9 @@ * - read_lattice_constant(): non-cubic named lattices require * LATTICE_PARAMETERS; without it the shape variables stayed zero and * produced a zero-volume or NaN cell (issue #8354) + * - TrigonalMalformedParamRejected + * - a failed cos(alpha) extraction must not silently become the + * simple-cubic (alpha = 90 degree) special case * - ValidHexagonalWithParams * - a hexagonal lattice with c/a given still reads normally * - CubicScNeedsNoParams @@ -85,6 +88,21 @@ TEST_F(ReadLatticeValidationTest, TrigonalMissingParamsRejected) ifa.close(); } +TEST_F(ReadLatticeValidationTest, TrigonalMalformedParamRejected) +{ + // a failed cos(alpha) read must not silently become the simple-cubic + // (alpha = 90 degree) special case + const std::string stru = + write_stru_file("trigonal_bad_param.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\nabc\n"); + lat.latName = "trigonal"; + std::ifstream ifa(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("invalid cos(alpha)")); + ifa.close(); +} + TEST_F(ReadLatticeValidationTest, ValidHexagonalWithParams) { const std::string stru = From 29bfe6660ff7c361b447b08b2aa268a699b12d29 Mon Sep 17 00:00:00 2001 From: DanielZhao0432 Date: Sat, 21 Nov 2026 23:50:56 +0800 Subject: [PATCH 3/7] Fix(cell): validate multi-value LATTICE_PARAMETERS reads in sm/bacm/triclinic Address review on #8354: a partially malformed parameter line such as '2 3 bad' leaves the earlier values in place and the failed one at zero, producing a finite nonzero-volume cell (e.g. sm gives a diag(1,2,3)-like lattice with det = 6) that the determinant check alone accepts. Read the parameter values with a single extraction chain and reject on failure. Add death tests for the three branches plus a valid sm control; the local test binary passes 10/10. --- source/source_cell/read_atom_species.cpp | 24 +++++-- .../unittests/test_read_atom_species.cpp | 62 ++++++++++++++++++- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/source/source_cell/read_atom_species.cpp b/source/source_cell/read_atom_species.cpp index ffe6320575d..9f4b40d77a4 100644 --- a/source/source_cell/read_atom_species.cpp +++ b/source/source_cell/read_atom_species.cpp @@ -348,8 +348,12 @@ bool read_lattice_constant(std::ifstream& ifa, double e21 = 0.0; double e22 = 0.0; if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { - ifa >> bba >> cba; - ModuleBase::GlobalFunc::READ_VALUE(ifa, cosab); + ifa >> bba >> cba >> cosab; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type sm"); + } e21 = bba * cosab; e22 = bba * sqrt(1.0-cosab*cosab); } @@ -364,8 +368,12 @@ bool read_lattice_constant(std::ifstream& ifa, double e21 = 0.0; double e22 = 0.0; if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { - ifa >> bba >> cba; - ModuleBase::GlobalFunc::READ_VALUE(ifa, cosab); + ifa >> bba >> cba >> cosab; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type bacm"); + } e21 = bba * cosab; e22 = bba * sqrt(1.0-cosab*cosab); cba = cba / 2.0; @@ -385,8 +393,12 @@ bool read_lattice_constant(std::ifstream& ifa, double term = 0.0; if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { - ifa >> bba >> cba >> cosab >> cosac; - ModuleBase::GlobalFunc::READ_VALUE(ifa, cosbc); + ifa >> bba >> cba >> cosab >> cosac >> cosbc; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type triclinic"); + } sinab = sqrt(1.0-cosab*cosab); } latvec.e11 = 1.0; latvec.e12 = 0.0; latvec.e13 = 0.0; diff --git a/source/source_cell/unittests/test_read_atom_species.cpp b/source/source_cell/unittests/test_read_atom_species.cpp index c6f82fc1070..4df61afde07 100644 --- a/source/source_cell/unittests/test_read_atom_species.cpp +++ b/source/source_cell/unittests/test_read_atom_species.cpp @@ -21,7 +21,12 @@ * - TrigonalMalformedParamRejected * - a failed cos(alpha) extraction must not silently become the * simple-cubic (alpha = 90 degree) special case - * - ValidHexagonalWithParams + * - SmPartiallyMalformedParamsRejected / BacmPartiallyMalformedParamsRejected + * / TriclinicPartiallyMalformedParamsRejected + * - a partially malformed multi-value LATTICE_PARAMETERS line (e.g. + * "2 3 bad") leaves a finite nonzero-volume cell, so the determinant + * check alone is not enough for these branches + * - ValidHexagonalWithParams / ValidSmWithParams * - a hexagonal lattice with c/a given still reads normally * - CubicScNeedsNoParams * - cubic lattice names do not need LATTICE_PARAMETERS @@ -115,6 +120,61 @@ TEST_F(ReadLatticeValidationTest, ValidHexagonalWithParams) ifa.close(); } +TEST_F(ReadLatticeValidationTest, SmPartiallyMalformedParamsRejected) +{ + // "2 3 bad": the first two values read fine and the failed cosine read + // leaves a finite diag(1,2,3)-like cell, which the determinant check + // alone would accept + const std::string stru = + write_stru_file("sm_partial_bad.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n2 3 bad\n"); + lat.latName = "sm"; + std::ifstream ifa(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("invalid LATTICE_PARAMETERS for lattice type sm")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, BacmPartiallyMalformedParamsRejected) +{ + const std::string stru = + write_stru_file("bacm_partial_bad.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n2 3 bad\n"); + lat.latName = "bacm"; + std::ifstream ifa(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("invalid LATTICE_PARAMETERS for lattice type bacm")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, TriclinicPartiallyMalformedParamsRejected) +{ + const std::string stru = write_stru_file("triclinic_partial_bad.stru", + "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n0.5 0.5 bad\n"); + lat.latName = "triclinic"; + std::ifstream ifa(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("invalid LATTICE_PARAMETERS for lattice type triclinic")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, ValidSmWithParams) +{ + const std::string stru = + write_stru_file("sm_valid.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n2 3 0.5\n"); + lat.latName = "sm"; + std::ifstream ifa(stru); + EXPECT_TRUE(unitcell::read_lattice_constant(ifa, ofs_running, lat)); + EXPECT_DOUBLE_EQ(lat.latvec.e21, 2.0 * 0.5); + EXPECT_DOUBLE_EQ(lat.latvec.e33, 3.0); + EXPECT_GT(lat.latvec.Det(), 0.0); + ifa.close(); +} + TEST_F(ReadLatticeValidationTest, CubicScNeedsNoParams) { const std::string stru = write_stru_file("sc_no_param.stru", "LATTICE_CONSTANT\n4.0\n"); From 6cb61e08f64850990b93d593fb71957bb263b1d5 Mon Sep 17 00:00:00 2001 From: DanielZhao0432 Date: Sun, 22 Nov 2026 01:49:59 +0800 Subject: [PATCH 4/7] Fix(cell): validate remaining LATTICE_PARAMETERS reads against range errors An out-of-range value such as 1e999 sets failbit but stores DBL_MAX, leaving a finite nonzero determinant that escaped both the unchecked READ_VALUE extraction and the determinant check. Every named-lattice parameter read (hexagonal, st, bct, so, baco, fco, bco) now validates extraction directly, matching the sm/bacm/triclinic branches. --- source/source_cell/read_atom_species.cpp | 53 +++++++++++++++---- .../unittests/test_read_atom_species.cpp | 34 ++++++++++++ 2 files changed, 76 insertions(+), 11 deletions(-) diff --git a/source/source_cell/read_atom_species.cpp b/source/source_cell/read_atom_species.cpp index 9f4b40d77a4..13c5a3d915c 100644 --- a/source/source_cell/read_atom_species.cpp +++ b/source/source_cell/read_atom_species.cpp @@ -238,7 +238,12 @@ bool read_lattice_constant(std::ifstream& ifa, latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0; if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { - ModuleBase::GlobalFunc::READ_VALUE(ifa, latvec.e33); + ifa >> latvec.e33; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type hexagonal"); + } } } else if(latName=="trigonal") @@ -276,7 +281,12 @@ bool read_lattice_constant(std::ifstream& ifa, latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0; if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { - ModuleBase::GlobalFunc::READ_VALUE(ifa, latvec.e33); + ifa >> latvec.e33; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type st"); + } } } else if(latName=="bct") @@ -284,7 +294,12 @@ bool read_lattice_constant(std::ifstream& ifa, double cba = 0.0; if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { - ModuleBase::GlobalFunc::READ_VALUE(ifa, cba); + ifa >> cba; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type bct"); + } cba = cba / 2.0; } latvec.e11 = 0.5; latvec.e12 =-0.5; latvec.e13 = cba; @@ -298,8 +313,12 @@ bool read_lattice_constant(std::ifstream& ifa, latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0; if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { - ifa >> latvec.e22; - ModuleBase::GlobalFunc::READ_VALUE(ifa, latvec.e33); + ifa >> latvec.e22 >> latvec.e33; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type so"); + } } } else if(latName=="baco") @@ -309,10 +328,14 @@ bool read_lattice_constant(std::ifstream& ifa, latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0; if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { - ifa >> latvec.e12; + ifa >> latvec.e12 >> latvec.e33; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type baco"); + } latvec.e12 = latvec.e12 / 2.0; latvec.e22 = latvec.e12; - ModuleBase::GlobalFunc::READ_VALUE(ifa, latvec.e33); } } else if(latName=="fco") @@ -320,8 +343,12 @@ bool read_lattice_constant(std::ifstream& ifa, double bba = 0.0; double cba = 0.0; if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { - ifa >> bba; - ModuleBase::GlobalFunc::READ_VALUE(ifa, cba); + ifa >> bba >> cba; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type fco"); + } bba = bba / 2.0; cba = cba / 2.0; } latvec.e11 = 0.5; latvec.e12 = 0.0; latvec.e13 = cba; @@ -333,8 +360,12 @@ bool read_lattice_constant(std::ifstream& ifa, double bba = 0.0; double cba = 0.0; if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { - ifa >> bba; - ModuleBase::GlobalFunc::READ_VALUE(ifa, cba); + ifa >> bba >> cba; + if (ifa.fail()) + { + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "invalid LATTICE_PARAMETERS for lattice type bco"); + } bba = bba / 2.0; cba = cba / 2.0; } latvec.e11 = 0.5; latvec.e12 = bba; latvec.e13 = cba; diff --git a/source/source_cell/unittests/test_read_atom_species.cpp b/source/source_cell/unittests/test_read_atom_species.cpp index 4df61afde07..3d9f2d43feb 100644 --- a/source/source_cell/unittests/test_read_atom_species.cpp +++ b/source/source_cell/unittests/test_read_atom_species.cpp @@ -28,6 +28,11 @@ * check alone is not enough for these branches * - ValidHexagonalWithParams / ValidSmWithParams * - a hexagonal lattice with c/a given still reads normally + * - SoRangeOverflowRejected / StRangeOverflowRejected + * - an out-of-range value such as 1e999 sets failbit but stores + * DBL_MAX, leaving a finite nonzero-volume cell that the determinant + * check alone would accept; every LATTICE_PARAMETERS extraction is + * therefore validated directly * - CubicScNeedsNoParams * - cubic lattice names do not need LATTICE_PARAMETERS * - DegenerateLatticeVectorsRejected @@ -175,6 +180,35 @@ TEST_F(ReadLatticeValidationTest, ValidSmWithParams) ifa.close(); } +TEST_F(ReadLatticeValidationTest, SoRangeOverflowRejected) +{ + // "1 1e999": the out-of-range read sets failbit but stores DBL_MAX, + // leaving det = DBL_MAX (finite and nonzero), so the determinant check + // alone would accept this malformed input + const std::string stru = + write_stru_file("so_range_overflow.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n1 1e999\n"); + lat.latName = "so"; + std::ifstream ifa(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("invalid LATTICE_PARAMETERS for lattice type so")); + ifa.close(); +} + +TEST_F(ReadLatticeValidationTest, StRangeOverflowRejected) +{ + const std::string stru = + write_stru_file("st_range_overflow.stru", "LATTICE_CONSTANT\n4.0\nLATTICE_PARAMETERS\n1e999\n"); + lat.latName = "st"; + std::ifstream ifa(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("invalid LATTICE_PARAMETERS for lattice type st")); + ifa.close(); +} + TEST_F(ReadLatticeValidationTest, CubicScNeedsNoParams) { const std::string stru = write_stru_file("sc_no_param.stru", "LATTICE_CONSTANT\n4.0\n"); From 66701cedf7cde111cff2a3c06a0010512e7c2e52 Mon Sep 17 00:00:00 2001 From: DanielZhao0432 Date: Sun, 22 Nov 2026 18:09:23 +0800 Subject: [PATCH 5/7] Fix(cell): report unsupported latname before requiring LATTICE_PARAMETERS The parameter-section guard ran before the lattice-name cascade, so an unsupported latname was met with a demand for LATTICE_PARAMETERS instead of 'latname not supported' (caught by legacy ReadAtomSpeciesWarning5). Move the guard after the branches so the name validates first. --- source/source_cell/read_atom_species.cpp | 27 ++++++++++++------------ 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/source/source_cell/read_atom_species.cpp b/source/source_cell/read_atom_species.cpp index edfd12fc99c..abc81d380e2 100644 --- a/source/source_cell/read_atom_species.cpp +++ b/source/source_cell/read_atom_species.cpp @@ -230,18 +230,6 @@ bool read_lattice_constant(std::ifstream& ifa, ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", "do not use LATTICE_VECTORS along with explicit specification of lattice type"); } - // Non-cubic named lattices cannot be constructed without the shape - // parameters in LATTICE_PARAMETERS; without them the shape variables - // stay zero and produce a zero-volume or NaN cell. - if(latName != "sc" && latName != "fcc" && latName != "bcc") - { - const bool has_params = scan_lattice_section(ifa, "LATTICE_PARAMETERS"); - if (!has_params) - { - const std::string msg = "LATTICE_PARAMETERS is required for lattice type " + latName; - ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", msg); - } - } if(latName=="sc") {//simple-cubic, ibrav = 1 latvec.e11 = 1.0; latvec.e12 = 0.0; latvec.e13 = 0.0; @@ -470,9 +458,22 @@ bool read_lattice_constant(std::ifstream& ifa, latvec.e33 = cba * term; } else - { + { ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant","latname not supported!"); } + // Non-cubic named lattices cannot be constructed without the shape + // parameters in LATTICE_PARAMETERS; without them the shape variables + // stay zero and produce a zero-volume or NaN cell. The check comes + // after the branches so an unsupported latName reports itself first. + if(latName != "sc" && latName != "fcc" && latName != "bcc") + { + const bool has_params = scan_lattice_section(ifa, "LATTICE_PARAMETERS"); + if (!has_params) + { + const std::string msg = "LATTICE_PARAMETERS is required for lattice type " + latName; + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", msg); + } + } } // Reject degenerate (zero-volume) or non-finite lattice vectors, whether From 6fe1f713ecdbf5da7d5b12c2fad6628993c3cdec Mon Sep 17 00:00:00 2001 From: DanielZhao0432 Date: Mon, 23 Nov 2026 07:46:57 +0800 Subject: [PATCH 6/7] Test(cell): pin the latname-then-params diagnostic order The legacy Warning5 fixture contains LATTICE_PARAMETERS, so it passes with the presence guard on either side of the name cascade. Add an unsupported-latName case without the section to lock in the order. --- .../unittests/test_read_atom_species.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/source/source_cell/unittests/test_read_atom_species.cpp b/source/source_cell/unittests/test_read_atom_species.cpp index 5c688db36e8..e0eb23ec81b 100644 --- a/source/source_cell/unittests/test_read_atom_species.cpp +++ b/source/source_cell/unittests/test_read_atom_species.cpp @@ -196,6 +196,22 @@ TEST_F(ReadLatticeValidationTest, TrigonalImpossibleAngleRejected) ifa.close(); } +TEST_F(ReadLatticeValidationTest, UnsupportedLatNameReportsItself) +{ + // an unsupported lattice name must report "latname not supported", not + // be asked for LATTICE_PARAMETERS: the presence guard runs after the + // name cascade. The legacy Warning5 fixture contains LATTICE_PARAMETERS, + // so it passes with either guard order and cannot pin this ordering. + const std::string stru = write_stru_file("arbitrary_no_param.stru", "LATTICE_CONSTANT\n4.0\n"); + lat.latName = "arbitrary"; + std::ifstream ifa = open_stru_file(stru); + testing::internal::CaptureStdout(); + EXPECT_EXIT(unitcell::read_lattice_constant(ifa, ofs_running, lat), ::testing::ExitedWithCode(1), ""); + const std::string output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("latname not supported")); + ifa.close(); +} + TEST_F(ReadLatticeValidationTest, ValidHexagonalWithParams) { const std::string stru = From 376024008a510341ae3328d14b81b93771cf47ed Mon Sep 17 00:00:00 2001 From: DanielZhao0432 Date: Mon, 23 Nov 2026 08:45:50 +0800 Subject: [PATCH 7/7] CI: retrigger transient ReadTheDocs build failure