瀏覽代碼

TextSubtext fixes (#4759)

Fix buffer write overflow
Fix reading past the end of text
veins1 7 月之前
父節點
當前提交
1d87932d93
共有 1 個文件被更改,包括 6 次插入6 次删除
  1. 6 6
      src/rtext.c

+ 6 - 6
src/rtext.c

@@ -1540,21 +1540,21 @@ const char *TextSubtext(const char *text, int position, int length)
 
     if (position >= textLength)
     {
-        position = textLength - 1;
-        length = 0;
+        return buffer; //First char is already '\0' by memset
     }
 
-    if (length >= textLength) length = textLength;
+    int maxLength = textLength - position;
+    if (length > maxLength) length = maxLength;
+    if (length >= MAX_TEXT_BUFFER_LENGTH) length = MAX_TEXT_BUFFER_LENGTH - 1;
 
     // NOTE: Alternative: memcpy(buffer, text + position, length)
 
     for (int c = 0 ; c < length ; c++)
     {
-        *(buffer + c) = *(text + position);
-        text++;
+        buffer[c] = text[position + c];
     }
 
-    *(buffer + length) = '\0';
+    buffer[length] = '\0';
 
     return buffer;
 }