struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / bug-3003.c
blob36da13620ec267ce67e29dce4c1a4eea3b164b56
1 /*
2 bug-3003.c
4 The wrong location is used for a local variable; observable effects vary by backend.
5 For stm8, this results in overwriting other local variables.
6 For z80, it results in use of location below the stack.
7 */
9 #include <testfwk.h>
11 #include <stdint.h>
12 #include <string.h>
14 #pragma disable_warning 88
16 void g(void)
20 static volatile uint8_t* f(const uint8_t id)
22 volatile uint8_t* result = 0; // Bug: For stm8, this variable is accessed at stack at sp+255, overwriting part of buffer.
23 switch (id)
25 case 0:
26 result = (volatile uint8_t *)0x5000;
27 break;
28 case 1:
29 result = (volatile uint8_t *)0x5005;
30 break;
31 case 2:
32 result = (volatile uint8_t *)0x500a;
33 break;
35 g(); // This call overwrites result by the return address for z80.
36 return result;
39 #if defined (__SDCC_pdk14) || defined (__SDCC_pdk15) || defined (__SDCC_mcs51) || defined (__SDCC_STACK_AUTO) // Lack of memory
40 #define BUFFERSIZE 20
41 #else
42 #define BUFFERSIZE 300
43 #endif
45 void testBug(void)
47 unsigned char buffer[BUFFERSIZE]; // Bug: Overwritten in f() on stm8.
49 memset(buffer, 0xa5, BUFFERSIZE);
51 ASSERT(f(0) == (volatile uint8_t *)0x5000);
52 ASSERT(f(1) == (volatile uint8_t *)0x5005);
53 ASSERT(f(2) == (volatile uint8_t *)0x500a);
55 for(size_t i = 0; i < BUFFERSIZE; i++)
56 ASSERT (buffer[i] == 0xa5);