From ef4af2a10438e417d22b967b738937b1869e003d Mon Sep 17 00:00:00 2001
From: Xavier Robin <xavalias-github@xavier.robin.name>
Date: Thu, 29 Jun 2023 16:47:48 +0200
Subject: [PATCH] fix: check length of counts line

This improves error messages when the counts line is invalid and the
calls to substr() return raw C++ errors.
---
 modules/io/src/mol/sdf_reader.cc | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/modules/io/src/mol/sdf_reader.cc b/modules/io/src/mol/sdf_reader.cc
index 3c28e279c..72488aa98 100644
--- a/modules/io/src/mol/sdf_reader.cc
+++ b/modules/io/src/mol/sdf_reader.cc
@@ -159,6 +159,11 @@ void SDFReader::ParseAndAddHeader(const String& line, int line_num,
       break;
     case 4:  // counts line
     {
+      if (line.length() < 39) {
+        String msg="Bad counts line %d: Not correct number of characters on "
+                   "the line: %i (should be at least 39)";
+        throw IOException(str(format(msg) % line_num % line.length()));
+      }
       String version_str=line.substr(34, 5);
       if (version_str != "V2000") {
         String msg="Unsupported SDF version: %s.";
-- 
GitLab