Skip to content

Commit

Permalink
A different solution for the overflow bug that replace the 2 function…
Browse files Browse the repository at this point in the history
… definitions by only one macro
  • Loading branch information
mingodad committed Dec 17, 2021
1 parent 1c36a2e commit 519a803
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
12 changes: 3 additions & 9 deletions src/api/mcfrelax.c
Expand Up @@ -24,13 +24,7 @@
#include "env.h"
#include "glpk.h"
#include "relax4.h"

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;
}
#include <limits.h>

int glp_mincost_relax4(glp_graph *G, int v_rhs, int a_low, int a_cap,
int a_cost, int crash, glp_double *sol, int a_x, int a_rc)
Expand Down Expand Up @@ -157,7 +151,7 @@ int glp_mincost_relax4(glp_graph *G, int v_rhs, int a_low, int a_cap,
/* substitute x = x' + low, where 0 <= x' <= cap - low */
csa.u[k] = (int)(cap - low);
/* correct demands at endpoints of k-th arc */
if (overflow(csa.dfct[a->tail->i], +low))
if (glp_int_addition_overflows(csa.dfct[a->tail->i], +low))
{ ret = GLP_ERANGE;
goto done;
}
Expand All @@ -166,7 +160,7 @@ int glp_mincost_relax4(glp_graph *G, int v_rhs, int a_low, int a_cap,
#else
csa.dfct[a->tail->i] += (int)low;
#endif
if (overflow(csa.dfct[a->head->i], -low))
if (glp_int_addition_overflows(csa.dfct[a->head->i], -low))
{ ret = GLP_ERANGE;
goto done;
}
Expand Down
4 changes: 4 additions & 0 deletions src/env/env.h
Expand Up @@ -327,6 +327,10 @@ void *xdlsym(void *h, const char *symbol);
void xdlclose(void *h);
/* close dynamically linked library */

/* check for integer overflow on computing u + v */
#define glp_int_addition_overflows(u, v) \
((u > 0 && v > INT_MAX - u) || (u < 0 && v < INT_MIN - u))

#endif

/* eof */
20 changes: 7 additions & 13 deletions src/misc/okalg.c
Expand Up @@ -23,6 +23,7 @@

#include "env.h"
#include "okalg.h"
#include <limits.h>

/***********************************************************************
* NAME
Expand Down Expand Up @@ -89,13 +90,6 @@
* Corp., Report R-375-PR (August 1962), Chap. III "Minimal Cost Flow
* Problems," pp.113-26. */

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;
}

int okalg(int nv, int na, const int tail[], const int head[],
const int low[], const int cap[], const int cost[], int x[],
int pi[])
Expand Down Expand Up @@ -147,7 +141,7 @@ int okalg(int nv, int na, const int tail[], const int head[],
aok = 0;
for (a = 1; a <= na; a++)
{ i = tail[a], j = head[a];
if (overflow(cost[a], pi[i] - pi[j]))
if (glp_int_addition_overflows(cost[a], pi[i] - pi[j]))
{ ret = 2;
goto done;
}
Expand Down Expand Up @@ -232,7 +226,7 @@ int okalg(int nv, int na, const int tail[], const int head[],
/* if the arc does not allow increasing the flow through
* it, skip the arc */
if (x[a] >= cap[a]) continue;
if (overflow(cost[a], pi[i] - pi[j]))
if (glp_int_addition_overflows(cost[a], pi[i] - pi[j]))
{ ret = 2;
goto done;
}
Expand All @@ -247,7 +241,7 @@ int okalg(int nv, int na, const int tail[], const int head[],
/* if the arc does not allow decreasing the flow through
* it, skip the arc */
if (x[a] <= low[a]) continue;
if (overflow(cost[a], pi[j] - pi[i]))
if (glp_int_addition_overflows(cost[a], pi[j] - pi[i]))
{ ret = 2;
goto done;
}
Expand All @@ -270,7 +264,7 @@ int okalg(int nv, int na, const int tail[], const int head[],
{ i = tail[a], j = head[a];
if (link[i] != 0 && link[j] == 0)
{ /* a = i->j, where node i is labelled, node j is not */
if (overflow(cost[a], pi[i] - pi[j]))
if (glp_int_addition_overflows(cost[a], pi[i] - pi[j]))
{ ret = 2;
goto done;
}
Expand All @@ -280,7 +274,7 @@ int okalg(int nv, int na, const int tail[], const int head[],
}
else if (link[i] == 0 && link[j] != 0)
{ /* a = j<-i, where node j is labelled, node i is not */
if (overflow(cost[a], pi[i] - pi[j]))
if (glp_int_addition_overflows(cost[a], pi[i] - pi[j]))
{ ret = 2;
goto done;
}
Expand All @@ -297,7 +291,7 @@ int okalg(int nv, int na, const int tail[], const int head[],
/* increase potentials of all unlabelled nodes */
for (i = 1; i <= nv; i++)
{ if (link[i] == 0)
{ if (overflow(pi[i], delta))
{ if (glp_int_addition_overflows(pi[i], delta))
{ ret = 2;
goto done;
}
Expand Down

0 comments on commit 519a803

Please sign in to comment.