From d72c0fb9a5103e807b6a08615c9b66635221434d Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Fri, 3 Sep 2021 18:10:27 -0500 Subject: [PATCH] Fix bug in some cases where a byte value is loaded and then stored as a word. It could wind up storing garbage in the upper 8 bits of the destination, because it was not doing a proper 8-bit to 16-bit conversion. This is an old bug, but the change in commit 95f518244212c caused it to be triggered in more cases, e.g. in the C7.5.1.1.CC test case. Here is a case that could exhibit the bug even before that: #pragma optimize 1 #include int main(void) { int k[1]; int i = 0; unsigned char uch = 'm'; k[i] = uch; printf("%i\n", k[0]); } --- Gen.pas | 12 ++++++++++++ cc.notes | 2 ++ 2 files changed, 14 insertions(+) diff --git a/Gen.pas b/Gen.pas index 1819491..5983d02 100644 --- a/Gen.pas +++ b/Gen.pas @@ -4278,6 +4278,18 @@ var GenNative(m_lda_abs, absolute, q, lab, 0) else GenNative(m_lda_long, longabsolute, q, lab, 0); + if not short then + if op^.right^.optype in [cgByte,cgUByte] then + if op^.right^.opcode <> pc_ldc then begin + GenNative(m_and_imm, immediate, $00FF, nil, 0); + if optype = cgByte then begin + GenNative(m_bit_imm, immediate, $0080, nil, 0); + lab1 := GenLabel; + GenNative(m_beq, relative, lab1, nil, 0); + GenNative(m_ora_imm, immediate, $FF00, nil, 0); + GenLab(lab1); + end; {if} + end; {if} end {if} else begin GenImplied(m_pla); diff --git a/cc.notes b/cc.notes index 06b3267..df71550 100644 --- a/cc.notes +++ b/cc.notes @@ -1213,6 +1213,8 @@ int foo(int[42]); 161. The system() function might return the wrong value when using the large memory model. (This was a regression introduced in ORCA/C 2.2.0 B3.) +162. In certain cases, an assignment from a location with an 8-bit type to a location with a 16-bit type might store the wrong value. + -- Bugs from C 2.1.0 that have been fixed ----------------------------------- 1. In some situations, fread() reread the first 1K or so of the file.