From 9998466d20545bdd8fca577a10d304c33b285914 Mon Sep 17 00:00:00 2001 From: Lew Rossman Date: Tue, 5 Nov 2019 10:09:21 -0500 Subject: [PATCH] Bug fix for EN_getrule Fixes possible seg fault condition in EN_getrule. Also defines EN_MISSING as an API constant since it can be assigned internally to several variables that are retrievable by the API. --- include/epanet2.bas | 4 +++- include/epanet2.pas | 9 +++++---- include/epanet2.vb | 4 +++- include/epanet2_enums.h | 3 ++- src/epanet.c | 22 +++++++++------------- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/include/epanet2.bas b/include/epanet2.bas index 6ac1044..4b9df87 100644 --- a/include/epanet2.bas +++ b/include/epanet2.bas @@ -5,7 +5,7 @@ Attribute VB_Name = "Module1" 'Declarations of functions in the EPANET PROGRAMMERs TOOLKIT '(EPANET2.DLL) -'Last updated on 10/29/2019 +'Last updated on 11/04/2019 ' These are codes used by the DLL functions Public Const EN_ELEVATION = 0 ' Node parameters @@ -239,6 +239,8 @@ Public Const EN_R_IS_OPEN = 1 ' Rule status types Public Const EN_R_IS_CLOSED = 2 Public Const EN_R_IS_ACTIVE = 3 +Public Const EN_MISSING As Double = -1.0E10 + 'These are the external functions that comprise the DLL 'Project Functions diff --git a/include/epanet2.pas b/include/epanet2.pas index e744744..58b2d4c 100644 --- a/include/epanet2.pas +++ b/include/epanet2.pas @@ -3,7 +3,7 @@ unit epanet2; { Declarations of imported procedures from the EPANET PROGRAMMERs TOOLKIT } { (EPANET2.DLL) } -{Last updated on 11/02/19} +{Last updated on 11/04/19} interface @@ -12,6 +12,7 @@ const { These are codes used by the DLL functions } EN_MAXID = 31; { Max. # characters in ID name } EN_MAXMSG = 255; { Max. # characters in strings } + EN_MISSING = -1.E10; EN_ELEVATION = 0; { Node parameters } EN_BASEDEMAND = 1; @@ -331,7 +332,7 @@ const function ENgetnumdemands(NodeIndex: Integer; var NumDemands: Integer): Integer; stdcall; external EpanetLib; function ENadddemand(NodeIndex: Integer; BaseDemand: Single; PatIndex: Integer; DemandName: PAnsiChar): Integer; stdcall; external EpanetLib; function ENdeletedemand(NodeIndex: Integer; DemandIndex: Integer): Integer; stdcall; external EpanetLib; - function ENgetdemandindex(NodeIndex: Integer; DemandName: PAnsiString; var DemandIndex: Integer): Integer; stdcall; external EpanetLib; + function ENgetdemandindex(NodeIndex: Integer; DemandName: PAnsiChar; var DemandIndex: Integer): Integer; stdcall; external EpanetLib; function ENgetbasedemand(NodeIndex: Integer; DemandIndex: Integer; var BaseDemand: Single): Integer; stdcall; external EpanetLib; function ENsetbasedemand(NodeIndex: Integer; DemandIndex: Integer; BaseDemand: Single): Integer; stdcall; external EpanetLib; function ENgetdemandpattern(NodeIndex: Integer; DemandIndex: Integer; var PatIndex: Integer): Integer; stdcall; external EpanetLib; @@ -394,11 +395,11 @@ const function ENsetcontrol(Index: Integer; Ctype: Integer; Link: Integer; Setting: Single; Node: Integer; Level: Single): Integer; stdcall; external EpanetLib; {Rule-Based Control Functions} - function ENaddrule(Rule: PAnsiString): Integer; stdcall; external EpanetLib; + function ENaddrule(Rule: PAnsiChar): Integer; stdcall; external EpanetLib; function ENdeleterule(Index: Integer): Integer; stdcall; external EpanetLib; function ENgetrule(Index: Integer; var Npremises: Integer; var NthenActions: Integer; var NelseActions: Integer; var Priority: Single): Integer; stdcall; external EpanetLib; - function ENgetruleID(Index: Integer; ID: PAnsiString): Integer; stdcall; external EpanetLib; + function ENgetruleID(Index: Integer; ID: PAnsiChar): Integer; stdcall; external EpanetLib; function ENsetrulepriority(Index: Integer; Priority: Single): Integer; stdcall; external EpanetLib; function ENgetpremise(RuleIndex: Integer; PremiseIndex: Integer; var LogOp: Integer; var ObjType: Integer; var ObjIndex: Integer; var Param: Integer; var RelOp: Integer; diff --git a/include/epanet2.vb b/include/epanet2.vb index e7c61e4..bfe65c2 100644 --- a/include/epanet2.vb +++ b/include/epanet2.vb @@ -4,7 +4,7 @@ 'Declarations of functions in the EPANET PROGRAMMERs TOOLKIT '(EPANET2.DLL) for use with VB.Net. -'Last updated on 10/29/2019 +'Last updated on 11/04/2019 Imports System.Runtime.InteropServices Imports System.Text @@ -244,6 +244,8 @@ Public Const EN_R_IS_OPEN = 1 ' Rule status types Public Const EN_R_IS_CLOSED = 2 Public Const EN_R_IS_ACTIVE = 3 +Public Const EN_MISSING As Double = -1.0E10 + 'These are the external functions that comprise the DLL 'Project Functions diff --git a/include/epanet2_enums.h b/include/epanet2_enums.h index 7c2e01a..ecfa12c 100644 --- a/include/epanet2_enums.h +++ b/include/epanet2_enums.h @@ -9,7 +9,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 07/22/2019 + Last Updated: 11/04/2019 ****************************************************************************** */ @@ -463,5 +463,6 @@ typedef enum { EN_R_IS_ACTIVE = 3 //!< Control valve is active } EN_RuleStatus; +#define EN_MISSING -1.E10 #endif //EPANET2_ENUMS_H diff --git a/src/epanet.c b/src/epanet.c index 4ba3c90..183027e 100644 --- a/src/epanet.c +++ b/src/epanet.c @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 11/02/2019 + Last Updated: 11/04/2019 ****************************************************************************** */ @@ -5227,34 +5227,30 @@ int DLLEXPORT EN_getrule(EN_Project p, int index, int *nPremises, if (index < 1 || index > net->Nrules) return 257; *priority = (double)p->network.Rule[index].priority; - count = 1; + count = 0; premise = net->Rule[index].Premises; - while (premise->next != NULL) + while (premise != NULL) { count++; premise = premise->next; } *nPremises = count; - count = 1; + count = 0; action = net->Rule[index].ThenActions; - while (action->next != NULL) + while (action != NULL) { count++; action = action->next; } *nThenActions = count; - action = net->Rule[index].ElseActions; count = 0; - if (action != NULL) + action = net->Rule[index].ElseActions; + while (action != NULL) { - count = 1; - while (action->next != NULL) - { - count++; - action = action->next; - } + count++; + action = action->next; } *nElseActions = count; return 0;