建立模型以避免泄漏_存储假阳性。

问题描述 投票:0回答:1

我正在努力解决一个泄漏的_存储假阳性问题。鉴于这个例子

struct string
{
  size_t len;
  data [1];
};

const char *string_new(const char *s, size_t n)
{
  struct string *st;

  st = malloc(offsetof(struct string, data) + n + 1);
  st->len = n;
  memcpy(st->data, s, len);
  st->data[len] = 0;
  return st->data;
}

void string_free(const char *s)
{
  struct string *st = (struct string *)(s - offsetof(struct string, data));
  free(st);
}

覆盖率扫描将报告

资源泄漏(RESOURCE_LEAK)5.泄漏的存储空间。变量st超出范围会泄露它所指向的存储。

在这个例子中,它直接显示不泄漏。怎么做才能让假阳性沉默?

2020-5-7日更新

建模不是正确的技术。Coverity Scan确实允许注释来解决这种特殊情况。在我们的案例中。

const char *string_new(const char *s, size_t n)
{
  struct string *st;

  st = malloc(offsetof(struct string, data) + n + 1);
  st->len = n;
  memcpy(st->data, s, len);
  st->data[len] = 0;
  /* coverity[leaked_storage] */
  return st->data;
}

会使假阳性警告沉默。

谢谢大家。

c static-analysis coverity
1个回答
0
投票

建模不是正确的技术。Coverity Scan确实允许注解来解决这种特殊情况。在我们的案例中。

const char *string_new(const char *s, size_t n)
{
  struct string *st;

  st = malloc(offsetof(struct string, data) + n + 1);
  st->len = n;
  memcpy(st->data, s, len);
  st->data[len] = 0;
  /* coverity[leaked_storage] */
  return st->data;
}

会使假阳性警告沉默。

谢谢大家。

© www.soinside.com 2019 - 2024. All rights reserved.