bpo-40998: Address compiler warnings found by ubsan (GH-20929)


Signed-off-by: Christian Heimes <christian@python.org>

Automerge-Triggered-By: GH:tiran
(cherry picked from commit 07f2adedf0940b06d136208ec386d69b7d2d5b43)

Co-authored-by: Christian Heimes <christian@python.org>
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index ea35903..ffd13f7 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -847,7 +847,11 @@
 
     /* generate replacement */
     for (i = collstart; i < collend; ++i) {
-        str += sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i));
+        size = sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i));
+        if (size < 0) {
+            return NULL;
+        }
+        str += size;
     }
     return str;
 }