closes bpo-39605: Fix some casts to not cast away const. (GH-18453)

gcc -Wcast-qual turns up a number of instances of casting away constness of pointers. Some of these can be safely modified, by either:

Adding the const to the type cast, as in:

-    return _PyUnicode_FromUCS1((unsigned char*)s, size);
+    return _PyUnicode_FromUCS1((const unsigned char*)s, size);

or, Removing the cast entirely, because it's not necessary (but probably was at one time), as in:

-    PyDTrace_FUNCTION_ENTRY((char *)filename, (char *)funcname, lineno);
+    PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);

These changes will not change code, but they will make it much easier to check for errors in consts
This commit is contained in:
Andy Lester 2020-02-11 20:28:35 -06:00 committed by GitHub
parent 029e8401b7
commit e6be9b59a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 39 additions and 39 deletions

View file

@ -172,8 +172,8 @@ extern "C" {
#define _PyUnicode_CONVERT_BYTES(from_type, to_type, begin, end, to) \
do { \
to_type *_to = (to_type *)(to); \
const from_type *_iter = (from_type *)(begin); \
const from_type *_end = (from_type *)(end); \
const from_type *_iter = (const from_type *)(begin);\
const from_type *_end = (const from_type *)(end);\
Py_ssize_t n = (_end) - (_iter); \
const from_type *_unrolled_end = \
_iter + _Py_SIZE_ROUND_DOWN(n, 4); \
@ -964,21 +964,21 @@ findchar(const void *s, int kind,
if ((Py_UCS1) ch != ch)
return -1;
if (direction > 0)
return ucs1lib_find_char((Py_UCS1 *) s, size, (Py_UCS1) ch);
return ucs1lib_find_char((const Py_UCS1 *) s, size, (Py_UCS1) ch);
else
return ucs1lib_rfind_char((Py_UCS1 *) s, size, (Py_UCS1) ch);
return ucs1lib_rfind_char((const Py_UCS1 *) s, size, (Py_UCS1) ch);
case PyUnicode_2BYTE_KIND:
if ((Py_UCS2) ch != ch)
return -1;
if (direction > 0)
return ucs2lib_find_char((Py_UCS2 *) s, size, (Py_UCS2) ch);
return ucs2lib_find_char((const Py_UCS2 *) s, size, (Py_UCS2) ch);
else
return ucs2lib_rfind_char((Py_UCS2 *) s, size, (Py_UCS2) ch);
return ucs2lib_rfind_char((const Py_UCS2 *) s, size, (Py_UCS2) ch);
case PyUnicode_4BYTE_KIND:
if (direction > 0)
return ucs4lib_find_char((Py_UCS4 *) s, size, ch);
return ucs4lib_find_char((const Py_UCS4 *) s, size, ch);
else
return ucs4lib_rfind_char((Py_UCS4 *) s, size, ch);
return ucs4lib_rfind_char((const Py_UCS4 *) s, size, ch);
default:
Py_UNREACHABLE();
}
@ -3420,7 +3420,7 @@ PyUnicode_Decode(const char *s,
/* Decode via the codec registry */
buffer = NULL;
if (PyBuffer_FillInfo(&info, NULL, (void *)s, size, 1, PyBUF_FULL_RO) < 0)
if (PyBuffer_FillInfo(&info, NULL, (const void *)s, size, 1, PyBUF_FULL_RO) < 0)
goto onError;
buffer = PyMemoryView_FromBuffer(&info);
if (buffer == NULL)
@ -4921,7 +4921,7 @@ ascii_decode(const char *start, const char *end, Py_UCS1 *dest)
/* Help allocation */
const char *_p = p;
while (_p < aligned_end) {
unsigned long value = *(unsigned long *) _p;
unsigned long value = *(const unsigned long *) _p;
if (value & ASCII_CHAR_MASK)
break;
_p += SIZEOF_LONG;
@ -5472,7 +5472,7 @@ PyUnicode_DecodeUTF32Stateful(const char *s,
PyObject *errorHandler = NULL;
PyObject *exc = NULL;
q = (unsigned char *)s;
q = (const unsigned char *)s;
e = q + size;
if (byteorder)
@ -5797,7 +5797,7 @@ PyUnicode_DecodeUTF16Stateful(const char *s,
PyObject *exc = NULL;
const char *encoding;
q = (unsigned char *)s;
q = (const unsigned char *)s;
e = q + size;
if (byteorder)
@ -6726,7 +6726,7 @@ PyUnicode_DecodeLatin1(const char *s,
const char *errors)
{
/* Latin-1 is equivalent to the first 256 ordinals in Unicode. */
return _PyUnicode_FromUCS1((unsigned char*)s, size);
return _PyUnicode_FromUCS1((const unsigned char*)s, size);
}
/* create or adjust a UnicodeEncodeError */
@ -13803,7 +13803,7 @@ _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer,
if (len == -1)
len = strlen(ascii);
assert(ucs1lib_find_max_char((Py_UCS1*)ascii, (Py_UCS1*)ascii + len) < 128);
assert(ucs1lib_find_max_char((const Py_UCS1*)ascii, (const Py_UCS1*)ascii + len) < 128);
if (writer->buffer == NULL && !writer->overallocate) {
PyObject *str;
@ -13862,7 +13862,7 @@ _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer,
{
Py_UCS4 maxchar;
maxchar = ucs1lib_find_max_char((Py_UCS1*)str, (Py_UCS1*)str + len);
maxchar = ucs1lib_find_max_char((const Py_UCS1*)str, (const Py_UCS1*)str + len);
if (_PyUnicodeWriter_Prepare(writer, len, maxchar) == -1)
return -1;
unicode_write_cstr(writer->buffer, writer->pos, str, len);