cov-scope.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Trigger Coverity WRAPPER_ESCAPE reporting
  3. *
  4. * We believe Coverity is misidentifying WRAPPER_ESCAPE in certain
  5. * circumstances, and this test program tries to evoke it.
  6. *
  7. * "Wrapper object use after free (WRAPPER_ESCAPE
  8. * escape: The internal representation of local `hel_w` escapes into
  9. * `prop[x].Value.lpszW`, but is destroyed when it exits scope."
  10. */
  11. #include <kopano/zcdefs.h>
  12. #include <kopano/platform.h>
  13. #include <string>
  14. #include <cstdlib>
  15. #include <mapidefs.h>
  16. using namespace KC;
  17. class I {
  18. public:
  19. virtual ~I(void) _kc_impdtor;
  20. /* ptr-to-non-const on purpose */
  21. virtual void SetProps(SPropValue *, size_t) {}
  22. };
  23. class X : public I {
  24. public:
  25. virtual void SetProps(SPropValue *, size_t) {}
  26. };
  27. class Y : public I {
  28. public:
  29. virtual void SetProps(SPropValue *, size_t) {}
  30. };
  31. int main(void)
  32. {
  33. SPropValue prop[8];
  34. std::string hel = "Hello World";
  35. std::wstring hel_w = L"Hello World";
  36. memset(prop, 0, sizeof(prop));
  37. prop[0].Value.lpszA = (char *)hel.c_str();
  38. prop[1].Value.lpszW = (WCHAR *)hel.c_str();
  39. prop[2].Value.lpszA = (char *)hel_w.c_str();
  40. prop[3].Value.lpszW = (WCHAR *)hel_w.c_str();
  41. prop[4].Value.lpszA = const_cast<char *>(hel.c_str());
  42. prop[5].Value.lpszW = reinterpret_cast<wchar_t *>(const_cast<char *>(hel.c_str()));
  43. prop[6].Value.lpszA = reinterpret_cast<char *>(const_cast<wchar_t *>(hel_w.c_str()));
  44. prop[7].Value.lpszW = const_cast<wchar_t *>(hel_w.c_str());
  45. I *obj;
  46. if (rand() & 1)
  47. obj = new X();
  48. else
  49. obj = new Y();
  50. obj->SetProps(prop, ARRAY_SIZE(prop));
  51. memset(prop, '\0', sizeof(prop));
  52. delete obj;
  53. return 0;
  54. }