Bug report
Bug description:
Static analysis with scan-build points to a potential use of an uninitialized variable in Objects/unicodeobject.c, specifically within the charmapencode_output function.
The variable unsigned char replace is declared on the stack without initialization. It is passed by reference to charmapencode_lookup(&replace). Later, inside the if (PyLong_Check(rep)) block, replace is cast and assigned to the output buffer:
outstart[(*outpos)++] = (char)replace; // scan-build: Assigned value is uninitialized
If charmapencode_lookup returns a valid PyLong object but fails to update the reference to replace (or purely from a static analysis perspective), this leads to reading uninitialized stack memory.
Suggested Fix:
Either initialize replace upon declaration or retrieve the value directly from the rep object, which is guaranteed to be a PyLong in that scope:
// Instead of using 'replace':
outstart[(*outpos)++] = (char)PyLong_AsLong(rep);
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Bug report
Bug description:
Static analysis with
scan-buildpoints to a potential use of an uninitialized variable inObjects/unicodeobject.c, specifically within thecharmapencode_outputfunction.The variable
unsigned char replaceis declared on the stack without initialization. It is passed by reference tocharmapencode_lookup(&replace). Later, inside theif (PyLong_Check(rep))block,replaceis cast and assigned to the output buffer:If
charmapencode_lookupreturns a validPyLongobject but fails to update the reference toreplace(or purely from a static analysis perspective), this leads to reading uninitialized stack memory.Suggested Fix:
Either initialize
replaceupon declaration or retrieve the value directly from therepobject, which is guaranteed to be aPyLongin that scope:CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux