2019-08-24 10:17:23 +02:00
|
|
|
|
|
|
|
// Test library configuration for python.cfg
|
|
|
|
//
|
|
|
|
// Usage:
|
2023-03-09 20:04:20 +01:00
|
|
|
// $ cppcheck --check-library --library=python --enable=style,information --inconclusive --error-exitcode=1 --disable=missingInclude --inline-suppr test/cfg/python.c
|
2019-08-24 10:17:23 +02:00
|
|
|
// =>
|
|
|
|
// No warnings about bad library configuration, unmatched suppressions, etc. exitcode=0
|
|
|
|
//
|
|
|
|
|
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
#include <Python.h> // should be the first include
|
2019-09-18 15:09:13 +02:00
|
|
|
#include <stdio.h>
|
2019-08-24 10:17:23 +02:00
|
|
|
|
|
|
|
void validCode(PyObject * pPyObjArg)
|
|
|
|
{
|
|
|
|
PyObject * pPyObjNULL = NULL;
|
|
|
|
Py_Initialize();
|
|
|
|
Py_INCREF(pPyObjArg);
|
|
|
|
Py_DECREF(pPyObjArg);
|
|
|
|
Py_XINCREF(pPyObjArg);
|
|
|
|
Py_XINCREF(pPyObjNULL);
|
|
|
|
Py_XDECREF(pPyObjArg);
|
|
|
|
Py_XDECREF(pPyObjNULL);
|
|
|
|
Py_CLEAR(pPyObjArg);
|
|
|
|
Py_CLEAR(pPyObjNULL);
|
|
|
|
(void)PyErr_NewException("text", NULL, NULL);
|
2019-09-18 15:09:13 +02:00
|
|
|
|
2023-05-21 14:01:14 +02:00
|
|
|
// cppcheck-suppress unusedAllocatedMemory
|
2019-09-18 15:09:13 +02:00
|
|
|
char * pBuf1 = PyMem_Malloc(5);
|
|
|
|
PyMem_Free(pBuf1);
|
2023-05-21 14:01:14 +02:00
|
|
|
// cppcheck-suppress unusedAllocatedMemory
|
2019-09-18 15:09:13 +02:00
|
|
|
int * pIntBuf1 = PyMem_New(int, 10);
|
|
|
|
PyMem_Free(pIntBuf1);
|
2019-08-24 10:17:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void nullPointer()
|
|
|
|
{
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
Py_INCREF(NULL);
|
|
|
|
// cppcheck-suppress nullPointer
|
|
|
|
Py_DECREF(NULL);
|
|
|
|
}
|
2019-09-18 15:09:13 +02:00
|
|
|
|
|
|
|
void PyMem_Malloc_memleak()
|
|
|
|
{
|
2023-08-05 18:48:43 +02:00
|
|
|
const char * pBuf1 = PyMem_Malloc(1);
|
2019-09-18 15:09:13 +02:00
|
|
|
printf("%p", pBuf1);
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|
|
|
|
|
|
|
|
void PyMem_Malloc_mismatchAllocDealloc()
|
|
|
|
{
|
2021-05-13 23:08:58 +02:00
|
|
|
// cppcheck-suppress unusedAllocatedMemory
|
2019-09-18 15:09:13 +02:00
|
|
|
char * pBuf1 = PyMem_Malloc(10);
|
|
|
|
// cppcheck-suppress mismatchAllocDealloc
|
|
|
|
free(pBuf1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PyMem_New_memleak()
|
|
|
|
{
|
2023-08-05 18:48:43 +02:00
|
|
|
const char * pBuf1 = PyMem_New(char, 5);
|
2019-09-18 15:09:13 +02:00
|
|
|
printf("%p", pBuf1);
|
|
|
|
// cppcheck-suppress memleak
|
|
|
|
}
|