Skip to content

Refactor IdCTypes unit for improved compatibility#18

Open
xjikka wants to merge 2 commits intomezen:masterfrom
xjikka:xjikka-patch-TIdC_TM
Open

Refactor IdCTypes unit for improved compatibility#18
xjikka wants to merge 2 commits intomezen:masterfrom
xjikka:xjikka-patch-TIdC_TM

Conversation

@xjikka
Copy link

@xjikka xjikka commented Jan 13, 2026

Fix ABI mismatch for struct tm on Linux (glibc)

On Linux/glibc (e.g. Raspberry Pi armhf), struct tm contains additional fields:

tm_gmtoff (long)
tm_zone (char*)

This makes sizeof(struct tm) 44 bytes, while current TIdC_TM is only 36 bytes (POSIX fields only).

Functions like ASN1_TIME_to_tm() write a full glibc struct tm. With the smaller Pascal record this causes stack corruption (observed as random overwrites of local variables, invalid TDateTime values such as 1899, etc.).

This PR extends TIdC_TM under {$IFDEF LINUX} to match the actual glibc layout and prevent memory corruption. No behavior change on non-Linux platforms.

Reproducible on:

Linux armhf (Raspberry Pi OS / Debian)
OpenSSL 1.1.1
Cross-compiled FPC binaries

Fix ABI mismatch for struct tm on Linux (glibc)

On Linux/glibc (e.g. Raspberry Pi armhf), struct tm contains additional fields:

tm_gmtoff (long)
tm_zone (char*)

This makes sizeof(struct tm) 44 bytes, while current TIdC_TM is only 36 bytes (POSIX fields only).

Functions like ASN1_TIME_to_tm() write a full glibc struct tm.
With the smaller Pascal record this causes stack corruption (observed as random overwrites of local variables, invalid TDateTime values such as 1899, etc.).

This PR extends TIdC_TM under {$IFDEF LINUX} to match the actual glibc layout and prevent memory corruption.
No behavior change on non-Linux platforms.

Reproducible on:

Linux armhf (Raspberry Pi OS / Debian)
OpenSSL 1.1.1
Cross-compiled FPC binaries
@xjikka
Copy link
Author

xjikka commented Jan 13, 2026

Example of the issue (FPC cross-compiled Win to Linux/ARM, Raspberry Pi)
When TIdC_TM is smaller than the actual glibc struct tm, calling X509.ValidFromInGMT may corrupt the stack.

Example code (FPC cross-compile to Raspberry Pi 3B, armhf):

nb  := 12345;
nbg := 11111;
nbg2 := 22222;

writeln('nb nbg nbg2 ', nb, ' ', nbg, ' ', nbg2);

nbg := x509.ValidFromInGMT;

writeln('nb nbg nbg2 ', nb, ' ', nbg, ' ', nbg2);

Output:

nb nbg nbg2 1.2345000000000000E+004 1.1111000000000000E+004 2.2222000000000000E+004
nb nbg nbg2 0.0000000000000000E+000 4.2543000000000000E+004 2.2222000000000000E+004

nb is unexpectedly overwritten after calling ValidFromInGMT, even though it is not modified by user code. This demonstrates stack corruption caused by ASN1_TIME_to_tm() writing a full glibc struct tm into a smaller Pascal record.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant