Bug description: In SU4, when using fread (so fd_read under the hood), it is consistently returning 0 for number of bytes read, along with the error number 29 regardless of if the operation actually succeeded.
Repro steps:
Try the following snippet in SU4
// Write to a test file and close it
FILE* handle = fopen("\\work\\test.dat", "w");
if (handle) {
fprintf(handle, "test");
fclose(handle);
}
// Open it again and try to read
handle = fopen("\\work\\test.dat", "r");
if (handle) {
char buffer[4];
int read = fread(buffer, 1, 4, handle);
printf("Succesfully read %d bytes: %s\n", read, buffer);
printf("errno = %d", errno);
fclose(handle);
}
The issue regarding fread setting errno to 29 has been fixed on our end - the fix will be available in 1.6.6.0 or higher (not in the next 1.6.5.0 to be published in the coming days).
After fixing this and implementing unit tests to avoid future regressions, we discovered that fopen would always set errno to 59 when used to open a file for writing. Upon investigating, we realized the issue came from the wasi-libc which we distribute with the SDK.
An issue has been opened on the wasi-libc gitHub and it looks like the bug is confirmed. We will let you know as soon as it is fixed and integrated in the MSFS SDK (we might fix it ourselves and send a PR to the team in charge). In the meantime, I suggest resetting errno to 0 manually after opening a file for writing.
As you can see the “read” variable is properly set to 4.
One note though: this bit of code is unsafe since we are doing a printf in the Log function with the “%s” format code on a non-null terminated string.
Anyway, it looks like the number of bytes read is now correct.
I read files but a bit different, via ifstream and write via ofstream. Those functions were affected in the same way? because I am also unable to read data now with SU4.
Glad to see you guys found out what is going on.. haha