Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory corruption with switch-local buffer #2440

Open
dfandrich opened this issue Nov 9, 2023 · 4 comments
Open

Memory corruption with switch-local buffer #2440

dfandrich opened this issue Nov 9, 2023 · 4 comments

Comments

@dfandrich
Copy link

A buffer placed in the scope of a switch statement results in memory corruption. The following code:

#include <stdio.h>
int main(void) {
   /* char switchbuf[32]; // Placing the buffer here works fine */
  int a=1234;
  printf("a: %d\n", a);
  switch (a) {
    char switchbuf[32];
    case 1234:
      printf("a: %d\n", a);
      break;
  }
  printf("a: %d\n", a);
  return 0;
}

results in the output:

a: 1234
a: 0
a: 0

when it should result in:

a: 1234
a: 1234
a: 1234

Moving the switchbuf variable outside the switch statement works as expected.

This is using the code from git from about 2 months ago.

@dfandrich
Copy link
Author

The test program was compiled with zcc +cpm -create-app switchvar.c I've confirmed that the problem still exists with at HEAD 162135b.

@suborb
Copy link
Member

suborb commented Nov 10, 2023

Impressive edge case - I've not come across code that does that.

It may take me a while to figure out how to solve this one nicely.

@dfandrich
Copy link
Author

Speaking of edge cases, I should see if zcc supports Duff's device ☺

@suborb
Copy link
Member

suborb commented Nov 10, 2023

I believe it does!

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

No branches or pull requests

2 participants