mirror of
https://github.com/openbabel/openbabel.git
synced 2026-08-09 12:38:03 -05:00
Fix fuzz bug on invalid (crazy coordinates) by clamping multiplies
Signed-off-by: Geoff Hutchison <geoff.hutchison@gmail.com>
This commit is contained in:
@@ -23,11 +23,24 @@ GNU General Public License for more details.
|
||||
#include <openbabel/elements.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
#include <climits>
|
||||
|
||||
using namespace std;
|
||||
namespace OpenBabel
|
||||
{
|
||||
|
||||
// Cast a double to int, clamping out-of-range or non-finite values.
|
||||
// Casting an out-of-range double to int is UB; UBSan flagged it on
|
||||
// fuzz inputs with absurd coordinates.
|
||||
static int chtClampToInt(double d)
|
||||
{
|
||||
if (!std::isfinite(d)) return 0;
|
||||
if (d > (double)INT_MAX) return INT_MAX;
|
||||
if (d < (double)INT_MIN) return INT_MIN;
|
||||
return (int)d;
|
||||
}
|
||||
|
||||
class CHTFormat : public OBMoleculeFormat
|
||||
{
|
||||
public:
|
||||
@@ -91,8 +104,8 @@ namespace OpenBabel
|
||||
vector<OBAtom*>::iterator i;
|
||||
for (atom = mol.BeginAtom(i); atom; atom = mol.NextAtom(i))
|
||||
{
|
||||
x = (int)(atom->GetX()) * conv_factor;
|
||||
y = (int)(atom->GetY()) * conv_factor;
|
||||
x = chtClampToInt(atom->GetX() * conv_factor);
|
||||
y = chtClampToInt(atom->GetY() * conv_factor);
|
||||
if (x > w)
|
||||
w = x;
|
||||
if (y > h)
|
||||
@@ -116,10 +129,10 @@ namespace OpenBabel
|
||||
bondtype = 3;
|
||||
// @todo: use flag-info, too
|
||||
snprintf(buffer, BUFF_SIZE, "%d\t%d\t%d\t%d\t%1d",
|
||||
(int)floor(atom1->GetX() * conv_factor + 0.5),
|
||||
(int)floor(atom1->GetY() * conv_factor + 0.5),
|
||||
(int)floor(atom2->GetX() * conv_factor + 0.5),
|
||||
(int)floor(atom2->GetY() * conv_factor + 0.5),
|
||||
chtClampToInt(floor(atom1->GetX() * conv_factor + 0.5)),
|
||||
chtClampToInt(floor(atom1->GetY() * conv_factor + 0.5)),
|
||||
chtClampToInt(floor(atom2->GetX() * conv_factor + 0.5)),
|
||||
chtClampToInt(floor(atom2->GetY() * conv_factor + 0.5)),
|
||||
bondtype);
|
||||
ofs << buffer << endl;
|
||||
}
|
||||
@@ -132,8 +145,8 @@ namespace OpenBabel
|
||||
if (atom->GetAtomicNum() != 6)
|
||||
{
|
||||
snprintf(buffer, BUFF_SIZE, "%d\t%d\t%s\t%d",
|
||||
(int)floor(atom->GetX() * conv_factor + 0.5),
|
||||
(int)floor(atom->GetY() * conv_factor + 0.5),
|
||||
chtClampToInt(floor(atom->GetX() * conv_factor + 0.5)),
|
||||
chtClampToInt(floor(atom->GetY() * conv_factor + 0.5)),
|
||||
OBElements::GetSymbol(atom->GetAtomicNum()),
|
||||
-1 // assume centered Text
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user