Menu

#476 For better portability, stop using the `long` type

unclassified
open
nobody
None
5 - default
2024-08-05
2024-08-01
No

A potential source of bugs in GnuCOBOL is the use of the long type , whose size differs in 64-bit MSVC (LLP64, long = 4 bytes) VS "most of the other compilers" (LP64, long = 8 bytes).

As an example, consider the code for cob_intr_random in intrinsic.c:

seed = get_seconds_past_midnight () * (long)COB_MODULE_PTR;

Under the 64-bit MSVC Debug target, the runtime checker displays a popup message complaining about the data loss induced by this cast (even if benign in this case).

Wherever the size matters - and where we can actually change the types (i.e. out of the libcob API) - I'd suggest using the C99 fixed width integer types (u)int64_t (or (u)intptr_t when casting from a pointer) - providing custom definitions for legacy compilers missing those. Alternatively we could just use cob_u64_t / cob_s64_t.

Discussion


Log in to post a comment.