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 95f5182442 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 <stdio.h>
int main(void) {
        int k[1];
        int i = 0;
        unsigned char uch = 'm';
        k[i] = uch;
        printf("%i\n", k[0]);
}
This commit is contained in:
Stephen Heumann 2021-09-03 18:10:27 -05:00
parent 3375e5ccc8
commit d72c0fb9a5
2 changed files with 14 additions and 0 deletions

12
Gen.pas
View File

@ -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);

View File

@ -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.