From 26351733bde20eb16aedd62b4b64a2417615020e Mon Sep 17 00:00:00 2001
From: Mamoru TASAKA <mtasaka@fedoraproject.org>
Date: Thu, 25 Nov 2021 23:07:31 +0900
Subject: [PATCH] MolfileReadCountsLine: fix storing n_atoms, n_bonds members
after reading short size
In MolfileReadCountsLine() (in INCHI_BASE/src/mol_fmt1.c), currently
ctab->n_atoms and ctab->n_bonds, which have int type (as defined in src/mol_fmt.h),
are passed to be MolfileReadField(), and to be treated there as if they have
short size (as MOL_FMT_SHORT_INT_DATA is also passed to the argument of
MolfileReadField(), and also the comment shows).
This causes a error on big endian system such as s390x, where for example
reading the line:
1 0 0 0 0 0 0 0 0 0999 V2000
On little endian (64bit) system, ctab->n_atoms is correctly stored as 1, but on
s390x (big endian 64bit) system, ctab->n_atoms gets 65536 (= 1<<16), which is
unexpected.
To fix this, once store the read value to short size local variable, then
copy the value to ctab->n_atoms or so later.
---
INCHI_BASE/src/mol_fmt1.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/INCHI_BASE/src/mol_fmt1.c b/INCHI_BASE/src/mol_fmt1.c
index 41ed15e..c2ad11a 100644
--- a/INCHI_BASE/src/mol_fmt1.c
+++ b/INCHI_BASE/src/mol_fmt1.c
@@ -572,6 +572,7 @@ int MolfileReadCountsLine( MOL_FMT_CTAB* ctab,
char line[MOL_FMT_INPLINELEN];
const int line_len = sizeof( line );
int err = 0, len;
+ short n_atoms, n_bonds;
p = inchi_fgetsLf( line, line_len, inp_file );
@@ -588,8 +589,8 @@ int MolfileReadCountsLine( MOL_FMT_CTAB* ctab,
TREAT_ERR( err, 0, "Too long counts line" ); /* too long input file line */
}
- if (0 > MolfileReadField( &ctab->n_atoms, 3, MOL_FMT_SHORT_INT_DATA, &p ) /* V2000 only: short int */
- || 0 > MolfileReadField( &ctab->n_bonds, 3, MOL_FMT_SHORT_INT_DATA, &p ) /* V2000 only: short int */
+ if (0 > MolfileReadField( &n_atoms, 3, MOL_FMT_SHORT_INT_DATA, &p ) /* V2000 only: short int */
+ || 0 > MolfileReadField( &n_bonds, 3, MOL_FMT_SHORT_INT_DATA, &p ) /* V2000 only: short int */
#if ( MOL_FMT_QUERY == MOL_FMT_PRESENT )
|| 0 > MolfileReadField( &ctab->n_atom_lists, 3, MOL_FMT_SHORT_INT_DATA, &p )
@@ -621,6 +622,8 @@ int MolfileReadCountsLine( MOL_FMT_CTAB* ctab,
AddErrorMessage( pStrErr, line );
goto err_fin;
}
+ ctab->n_atoms = n_atoms;
+ ctab->n_bonds = n_bonds;
/* Get CTFile version (V2000 or other) */
len = MolfileReadField( ctab->version_string,
--
2.33.1