llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[LifetimeSafety] Support C compound literals

Open NeKon69 opened this issue 5 months ago • 3 comments

LifetimeSafety does not fully model C compound literals yet.

Minimal repro:

int *bad(void) {
  int *p = &(int){12};
  return p; // should warn
}

https://godbolt.org/z/6oPvfr9bn

In C, compound literal has automatic storage duration, so this should behave roughly like returning the address of an unnamed local variable.

Expected: warn that the returned pointer refers to storage that does not live long enough.

This should remain OK however:

int ok(void) {
  int *p = &(int){12};
  *p = 100;
  return *p;
}

NeKon69 avatar Jun 28 '26 16:06 NeKon69

@llvm/issue-subscribers-clang-temporal-safety

Author: NeKon69

LifetimeSafety does not fully model C compound literals yet.

Minimal repro:

int *bad(void) {
  int *p = &(int){12};
  return p; // should warn
}

https://godbolt.org/z/6oPvfr9bn

In C, compound literal has automatic storage duration, so this should behave roughly like returning the address of an unnamed local variable.

Expected: warn that the returned pointer refers to storage that does not live long enough.

This should remain OK however:

int ok(void) {
  int *p = &(int){12};
  *p = 100;
  return *p;
}

This appears to be much harder than I initially anticipated as these are not modeled on the CFG level. So this might actually take a while..

NeKon69 avatar Jun 28 '26 17:06 NeKon69

I don't plan on working on this for the foreseeable future, there are too much blockers that I encountered while looking for a way to solve this issue.

NeKon69 avatar Sep 25 '26 17:09 NeKon69