bug-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Function overflow relies on undefined behavior


From: Caio Giuliani
Subject: Function overflow relies on undefined behavior
Date: Wed, 15 Dec 2021 12:01:28 -0300

Greetings,

Files src/misc/okalg.c and src/api/mcfrelax.c define the following function:
static int overflow(int u, int v)
{     /* check for integer overflow on computing u + v */
      if (u > 0 && v > 0 && u + v < 0) return 1;
      if (u < 0 && v < 0 && u + v > 0) return 1;
      return 0;
}
It contains integer overflow, which is undefined behavior by the C standard.
As I tested, both GCC 11.1.0 and clang 13.0.0 make this function always return 0 from optimization level O1 onwards.
Particularly function glp_mincost_relax4 appears to have received ad-hoc patches on the places this function is used.

I propose, instead, the following:
#include <limits.h>
static int overflow(int u, int v)
{     /* check for integer overflow on computing u + v */
      return (u > 0 && v > INT_MAX - u) return 1;
      if (u < 0 && v < INT_MIN - u) return 1;
      return 0;
}


Best regards,

--
Caio Merlini Giuliani
Operations Research Analyst
WPLEX Software Ltda
www.wplex.com.br

reply via email to

[Prev in Thread] Current Thread [Next in Thread]