https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99098

Peter Ross <pross at xvid dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pross at xvid dot org

--- Comment #2 from Peter Ross <pross at xvid dot org> ---
The following test case produces a -Wfree-nonheap-object false positive. I
argue that the memory being free'd is heap memory. It is offset by one to
accomodate the negative offset applied immediately after malloc.

```
#include <stdlib.h>
char * knn_alloc()
{
    char * w = malloc(sizeof(char));
    if (!w)
        return NULL;
    return w - 1;
}
void knn_free(char * w)
{
    free(w + 1);
}
int main()
{
    char * w = knn_alloc();
    if (!w)
        return -1;

    knn_free(w);
    return 0;
}
```

```
$ gcc knn.c -save-temps
knn.c: In function ‘knn_free’:
knn.c:11:5: warning: ‘free’ called on pointer ‘w’ with nonzero offset 1
[-Wfree-nonheap-object]
   11 |     free(w + 1);
      |     ^~~~~~~~~~~

```

gcc --version: gcc (Debian 13.2.0-2) 13.2.0
uname -a: Linux computer 6.4.0-3-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.4.11-1
(2023-08-17) x86_64 GNU/Linux

Reply via email to