소스 검색

[rtext] TextFormat() warn user if buffer overflow occured. (#3399)

* [rtext] TextFormat now alerts user to truncation.

* Update rtext.c

* Update rcore.c

* Update rtext.c
pull/3400/head
Murlocohol 1 년 전
committed by GitHub
부모
커밋
28fb58f0ea
No known key found for this signature in database GPG 키 ID: 4AEE18F83AFDEB23
2개의 변경된 파일21개의 추가작업 그리고 2개의 파일을 삭제
  1. +11
    -1
      src/rcore.c
  2. +10
    -1
      src/rtext.c

+ 11
- 1
src/rcore.c 파일 보기

@ -2922,6 +2922,7 @@ static void PlayAutomationEvent(unsigned int frame)
#if !defined(SUPPORT_MODULE_RTEXT)
// Formatting of text with variables to 'embed'
// WARNING: String returned will expire after this function is called MAX_TEXTFORMAT_BUFFERS times
const char *TextFormat(const char *text, ...)
{
#ifndef MAX_TEXTFORMAT_BUFFERS
@ -2940,12 +2941,21 @@ const char *TextFormat(const char *text, ...)
va_list args;
va_start(args, text);
vsnprintf(currentBuffer, MAX_TEXT_BUFFER_LENGTH, text, args);
kt">int charCountRequired = vsnprintf(currentBuffer, MAX_TEXT_BUFFER_LENGTH, text, args);
va_end(args);
// If charCountRequired is larger than the MAX_TEXT_BUFFER_LENGTH, then overflow occured
if(charCountRequired > MAX_TEXT_BUFFER_LENGTH)
{
// We are going to insert [TRUN] at the end of the string so the user knows what happened
char *truncBuffer = buffers[index] + MAX_TEXT_BUFFER_LENGTH - 7; // 7 = six letters + '\0'
sprintf(truncBuffer, "[TRUN]");
}
index += 1; // Move to next buffer for next function call
if (index >= MAX_TEXTFORMAT_BUFFERS) index = 0;
return currentBuffer;
}
#endif // !SUPPORT_MODULE_RTEXT

+ 10
- 1
src/rtext.c 파일 보기

@ -1371,15 +1371,24 @@ const char *TextFormat(const char *text, ...)
va_list args;
va_start(args, text);
vsnprintf(currentBuffer, MAX_TEXT_BUFFER_LENGTH, text, args);
kt">int charCountRequired = vsnprintf(currentBuffer, MAX_TEXT_BUFFER_LENGTH, text, args);
va_end(args);
// If charCountRequired is larger than the MAX_TEXT_BUFFER_LENGTH, then overflow occured
if(charCountRequired > MAX_TEXT_BUFFER_LENGTH)
{
// We are going to insert [TRUN] at the end of the string so the user knows what happened
char *truncBuffer = buffers[index] + MAX_TEXT_BUFFER_LENGTH - 7; // 7 = six letters + '\0'
sprintf(truncBuffer, "[TRUN]");
}
index += 1; // Move to next buffer for next function call
if (index >= MAX_TEXTFORMAT_BUFFERS) index = 0;
return currentBuffer;
}
// Get integer value from text
// NOTE: This function replaces atoi() [stdlib.h]
int TextToInteger(const char *text)

불러오는 중...
취소
저장