[rcore] Make FileText*() error handling consistent with Text*() helpers - #6210
Merged
Merged
Conversation
Owner
|
@Ne0nWinds thanks for the review! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR addresses two issues:
FileTextFindIndexwill dereference aNULLpointer in the following two cases:FileTextReplacewill completely overwrite the file contents with garbage if:""FileTextFindIndex
If a file exists but has empty contents,
LoadFileTextreturns a null pointer.FileTextFindIndexdirectly passes this null pointer intostrstr, which crashes the program. This is different fromTextFindIndex, which correctly handles zero-length strings.The solution here is to simply check if
LoadFileTextreturnsNULL. This makesFileTextFindIndexandTextFindIndexbehave consistently with empty strings, in addition to being more robust against failed file loads.FileTextReplace
If the file is empty or if the
searchparameter is a zero-length string,FileTextReplacepassesNULLdirectly intoSaveFileText. ThisNULLpointer gets passed intofprintfwith the format string%s. This is undefined behavior in C and does different things depending on the compiler and optimization level. MSVC, for example, writes out the literal string(null)to the file (completely overwriting whatever was there before).Furthermore, in the event that
FileTextReplacehits one of these failure cases, it returns0, so the caller has no way of knowing something went wrong.FileTextReplaceusesTextReplaceAlloc, which already handles all these edge cases gracefully, so the solution is to check ifTextReplaceAllocreturnsNULL. Doing so makesFileTextReplacehave consistent error handling withTextReplaceAlloc.