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 EOF detection in p4est_connectivity_getline_upper on Windows #115

Merged

Conversation

sloede
Copy link
Contributor

@sloede sloede commented May 29, 2021

Fix EOF detection in p4est_connectivity_getline_upper on Windows

We have noted that p4est_connectivity_getline_upper sometimes fails on Windows (not always - it seems to depend on the actual C library version used). The problem is rooted in the following lines:

p4est/src/p4est_connectivity.c

Lines 4598 to 4603 in 3a78fd2

c = fgetc (stream);
c = toupper (c);
if (c == EOF && linep == line) {
P4EST_FREE (linep);
return NULL;
}

After reading the next character from the stream with fgetc, it is converted to uppercase using toupper and then checked for the EOF (end of file) marker. This is OK on Linux (and presumably macOS), since

If c is a lowercase letter, toupper() returns its uppercase equivalent, if an uppercase representation exists in the current locale. Otherwise, it returns c.

(ref). Thus, if c is actually EOF (typically represented as -1), there is no uppercase representation and it remains unchanged.

However, on Windows the documentation says

In order for toupper to give the expected results, __isascii and islower must both return nonzero.

Thus, implicitly it says: If it is not an ASCII character, anything can happen! And indeed: In some cases

toupper(-1) == -1 == EOF

but in other cases (in my case when invoking libp4est from Julia),

toupper(-1) == 159 != EOF

and thus the reading loop in p4est_connectivity_getline_upper never terminates.

Proposed changes:
Move the conversion toupper to after the check for EOC, which will make this work no matter on which platform this is running.

@cburstedde
Copy link
Owner

cburstedde commented Jun 1, 2021

Thanks for checking this; can I ask you to include a doc/author_schlottke.txt file like the others? -- Ah it's in the other PR.

@sloede
Copy link
Contributor Author

sloede commented Jun 1, 2021

Sure! Done in 6fe8950.

@cburstedde
Copy link
Owner

cburstedde commented Jun 1, 2021

Thanks! I'll merge into master here, usually we ask for the develop branch.
Looking forward to more testing on Windows. :)

@cburstedde cburstedde merged commit 461decc into cburstedde:master Jun 1, 2021
@sloede sloede deleted the msl/fix-end-of-file-detection-windows branch June 1, 2021 18:39
@sloede
Copy link
Contributor Author

sloede commented Jun 1, 2021

usually we ask for the develop branch

Oops, sorry. I'll try to remember next time!

@cburstedde
Copy link
Owner

No worries and thanks again!

@cburstedde
Copy link
Owner

cburstedde commented Jun 2, 2021 via email

@sloede
Copy link
Contributor Author

sloede commented Jun 8, 2021

Speaking of next time, how hard would it be to incorporate the changes for
Windows from the install document into the code using proper #ifdefs?

I am not sure, to be honest, since in my experience there does not exist "the" Windows toolchain (as opposed to, say, Linux/macOS). Most Windows-native projects I've seen (admittedly not that many) seem to rely on MS Visual Studio compilers (cl, or clang-cl). Then there are cross-compilation setups such as BinaryBuilder, which IIR run on AlpineLinux and use either a clang or gcc-based toolchain. There's also Windows + mingw-w64, which I have used to debug stuff on Windows. The GitHub Action toolchain for windows-latest seems to offer a mixture of these options.

Each toolchain seems to have different requirements (I used BinaryBuilder and mingw with gcc). Going through the current examples in https://github.com/cburstedde/p4est/blob/master/INSTALL_WINDOWS, I have had the following experience:

While I would really appreciate having these "fixes" be applied automatically on Windows, I am not sure how to go about it such that it works for everyone. I guess one could probably figure this out during CMake preprocessing, but this will have to wait until you retire automake in favor of CMake builds, I guess.

@cburstedde
Copy link
Owner

cburstedde commented Jun 8, 2021 via email

@sloede
Copy link
Contributor Author

sloede commented Jun 8, 2021

I personally wouldn't get to testing it though.

If it passes Windows CI here, I can give it a shot on my personal Windows + mingw-w64 installation and/or using BinaryBuilder (for cross-compilation).

@cburstedde
Copy link
Owner

We don't have an autotools/mingw-based CI setup here, yet... but any testing is appreciated.

Right now playing with the feature-random branch to sort some of the above issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants