dss-extensions / dss_capi

A multi-platform, customized C-API library and alternative implementation of OpenDSS (EPRI Distribution System Simulator) aiming for full COM compatibility. Unofficial/unsupported by EPRI, but extensively tested.
https://dss-extensions.org/
BSD 3-Clause "New" or "Revised" License
33 stars 13 forks source link

Parser truncates values #95

Open PMeira opened 3 years ago

PMeira commented 3 years ago

The OpenDSS parser uses the Val function, e.g.

https://github.com/dss-extensions/dss_capi/blob/0ef72db7eb046ad5d7a3731ff5ea33b8089b728c/src/Parser/ParserDel.pas#L792

Most integers in OpenDSS are 32-bit values. If the input is larger than the max value, the value is truncated (upper bits dropped).

The results in DSS C-API are the same as in the official binaries, but we could add optional error messages to warn of this behavior.

Since the values are truncated instead of clipped to max/min, this can be dangerous.

Low priority.

PMeira commented 3 years ago

TryStrToInt is an alternative but there's an issue in current FPC versions: https://gitlab.com/freepascal.org/fpc/source/-/issues/39406

Using the native range checking should be enough in Free Pascal, without affecting the whole thing:

Remark The standard functions val and Read will also check ranges when the call is compiled in {$R+} mode. In Delphi, range checking is only switchable at the procedure level. In Free Pascal, the {$R } directive can be used at the expression level.