Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite while loop on empty added file. #90588

Conversation

ajreckof
Copy link
Member

In case an empty file was added in a commit copyright_headers would enter in an infinite loop as the file only contains empty lines which the scripts try to remove. Added a check to make sure that it would exit the loop it cases it reaches the last line.
this happened in #82198

@ajreckof ajreckof requested a review from a team as a code owner April 12, 2024 17:41
@AThousandShips AThousandShips added bug topic:buildsystem cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release labels Apr 12, 2024
@AThousandShips AThousandShips added this to the 4.3 milestone Apr 12, 2024
@AThousandShips
Copy link
Member

I'm not familiar with the way python does line reading, I'm assuming this preserves the original behaviour, and that "empty" lines are actually "\n"?

@ajreckof
Copy link
Member Author

ajreckof commented Apr 12, 2024

yes empty lines at least have the "\n" except if they are the last line. So effectively "" is only reached in two cases :

  • trying to read past the last line
  • reading the last line which is empty (file ends with new line)

those two cases clearly indicates we have reached the end of the file and there is nothing more to skip (because there is nothing more at all)

@@ -73,7 +73,7 @@
line = fileread.readline()
header_done = False

while line.strip() == "": # Skip empty lines at the top
while line.strip() == "" and line != "": # Skip empty lines at the top
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the recommended way to parse files in Python is

while fileread:

not

while fileread.readline() != "":

I'd recommend changing the code to something like

while fileread:
    line = fileread.readline()
    if line == "":
        continue

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how file works in python the fileread variable will not be considered as false when end of file is reached. This means while fileread: will be an infinite loop. Moreover here we want to remove only the empty lines at the start of the file which is exactly what it was doing. Code was ok just didn't think of empty file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this should probably be rewritten to use for loops instead of while loops. It looks like for line in file is the recommended way to iterate through a file line by line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact I agree that for loop are more idiomatic in file reading in pythonbut both can be found, moreover here changing the while loop wouldn't be recommended because we are first skipping lines until we find a non empty line then skipping until we find a non header line. Those until are for me a sign that while loops are more appropriate. Moreover I don't think a refactor of the file would be a good idea when the fix is simply modifying one line and therefore has no link with the refactor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I tend to get scared of while loops that don't have some guaranteed termination condition, but it's hard to argue with the simplicity of a one line fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the line != "" is in fact the guaranteed termination because we are sure it will evaluate to false at some point because the file as a finite number of lines and each loop will consume one line hence it will either terminate the loop because of the other condition or reach the end of the file and terminate the loop because of this condition.

@akien-mga akien-mga merged commit 4bce45b into godotengine:master Apr 13, 2024
16 checks passed
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cherrypick:4.1 Considered for cherry-picking into a future 4.1.x release cherrypick:4.2 Considered for cherry-picking into a future 4.2.x release topic:buildsystem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants