cburstedde / p4est

The "p4est" forest-of-octrees library
www.p4est.org/
GNU General Public License v2.0
261 stars 115 forks source link

Ensure C standard compliant `fseek` usage #298

Closed tim-griesbach closed 5 months ago

tim-griesbach commented 8 months ago

Ensure C standard compliant fseek usage

According to the C standard (cf. https://wiki.sei.cmu.edu/confluence/display/c/FIO19-C.+Do+not+use+fseek()+and+ftell()+to+compute+the+size+of+a+regular+file), it can not be assumed that seeking to the end of a binary file works in general. However, this was done in p4est_save_ext. In concrete terms, the C standard states that

Setting the file position indicator to end-of-file, as with fseek(file, 0, SEEK_END), has undefined behavior for a binary stream (because of possible trailing null characters) or for any stream with state-dependent encoding that does not assuredly end in the initial shift state.

Proposed changes: This PR ensures that it is not necessary to seek to the end of a binary file by not closing the file in p4est_save_ext after the connectivity was written to disk.

tim-griesbach commented 7 months ago

I removed the occurrences of sink->file by using p4est_connectivity_sink in p4est_save_ext.

tim-griesbach commented 7 months ago

sc_test_version failed in one CI test. This should be unrelated to the changes in this PR and transient.

Edit: Indeed, after rerunning the CI the failure disappeared.