Commit 7ec5fb21 authored by Alexander Stukowski's avatar Alexander Stukowski

Added additional validation check to XYZ frame discovery function to not…

Added additional validation check to XYZ frame discovery function to not mistakingly accept invalid files
parent 15907899
Pipeline #21915502 passed with stage
in 10 minutes 56 seconds
......@@ -117,7 +117,6 @@ void XYZImporter::FrameFinder::discoverFramesInFile(QFile& file, const QUrl& sou
// Regular expression for whitespace characters.
QRegularExpression ws_re(QStringLiteral("\\s+"));
int numParticles = 0;
QFileInfo fileInfo(stream.device().fileName());
QString filename = fileInfo.fileName();
QDateTime lastModified = fileInfo.lastModified();
......@@ -131,8 +130,17 @@ void XYZImporter::FrameFinder::discoverFramesInFile(QFile& file, const QUrl& sou
stream.readLine();
if(stream.line()[0] == '\0') break;
if(sscanf(stream.line(), "%u", &numParticles) != 1 || numParticles < 0 || numParticles > 1e9)
throw Exception(tr("Invalid number of particles in line %1 of XYZ file: %2").arg(stream.lineNumber()).arg(stream.lineString()));
unsigned long long numParticlesLong;
int charCount;
if(sscanf(stream.line(), "%llu%n", &numParticlesLong, &charCount) != 1)
throw Exception(tr("Invalid number of particles in line %1 of XYZ file: %2").arg(stream.lineNumber()).arg(stream.lineString().trimmed()));
// Check trailing whitespace. There should be nothing else but the number of atoms on the first line.
for(const char* p = stream.line() + charCount; *p != '\0'; ++p) {
if(!isspace(*p))
throw Exception(tr("Parsing error in line %1 of XYZ file. According to the XYZ format specification, the first line of a frame section must contain just the number of particles. This is not a valid integer number:\n\n\"%2\"").arg(stream.lineNumber()).arg(stream.lineString().trimmed()));
}
// Create a new record for the time step.
Frame frame;
......@@ -147,7 +155,7 @@ void XYZImporter::FrameFinder::discoverFramesInFile(QFile& file, const QUrl& sou
stream.readLine();
// Skip atom lines.
for(int i = 0; i < numParticles; i++) {
for(unsigned long long i = 0; i < numParticlesLong; i++) {
stream.readLine();
if((i % 4096) == 0)
setProgressValue(stream.underlyingByteOffset() / 1000);
......
......@@ -25,3 +25,9 @@ H 14.305226602 21.39307514 8.40018912 1.12
H 15.544237628 16.95278896 11.18835758 1.12
H 14.312762658 18.72276706 9.3899546 1.12
H 16.146983882 18.63601754 11.12001722 1.12
4
Lattice="22.0 0.0 0.0 0.0 22.0 0.0 0.0 0.0 22.0" Properties=species:S:1:pos:R:3:myprop:R:1
Li 14.093913126 20.56976562 7.45633742 1.12
Li 15.884056646 17.77985542 10.20637108 1.12
H 16.207558528 16.00336356 1.25815334 1.12
H 19.297242356 14.22386672 1.2752139 1.12
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment