Bug in C++ version of fread
-
- DC Developer
- Posts: 414
- https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
- Joined: Thu Aug 20, 2009 11:00 am
- Has thanked: 0
- Been thanked: 2 times
Bug in C++ version of fread
While working on a C++ program with the current KOS, I ran into a problem - fread() doesn't work for jack. It sometimes works, and many times doesn't. There's no rhyme or reason to be found - it just fails whenever it feels like it. It works fine in C programs - not a hitch at all. What I've done at this point is switch the C++ program to fs_read(), which works just fine. I'll probably leave it, but someone might want to look into this bug. I'm leaving this mainly as a warning to others working in C++: DON'T USE FREAD!!!
- BlueCrab
- The Crabby Overlord
- Posts: 5658
- Joined: Mon May 27, 2002 11:31 am
- Location: Sailing the Skies of Arcadia
- Has thanked: 9 times
- Been thanked: 69 times
- Contact:
Re: Bug in C++ version of fread
Considering that there is no difference between fread() in C or C++ (other than that it is put in the std:: namespace in C++ when you include <cstdio> instead of <stdio.h>), I think there's something else afoot here. There's no good reason that it should work in one and not the other otherwise...
EDIT: Just so I can have some idea of where to look, is the issue exhibiting itself when using the same file pointer for a long period of time, or is it only when opening and closing different files? Is it exhibiting itself with only one file open at a time, or with multiple? Also, what filesystem are you trying to read from (/cd, /rd, /pc, etc)? Basically, I need a lot more information to have any idea where to look...
EDIT: Just so I can have some idea of where to look, is the issue exhibiting itself when using the same file pointer for a long period of time, or is it only when opening and closing different files? Is it exhibiting itself with only one file open at a time, or with multiple? Also, what filesystem are you trying to read from (/cd, /rd, /pc, etc)? Basically, I need a lot more information to have any idea where to look...
- BlueCrab
- The Crabby Overlord
- Posts: 5658
- Joined: Mon May 27, 2002 11:31 am
- Location: Sailing the Skies of Arcadia
- Has thanked: 9 times
- Been thanked: 69 times
- Contact:
Re: Bug in C++ version of fread
For reference, I ran over 2000 passes of this piece of code without any issue:
Code: Select all
#include <cstdio>
#include <iostream>
#include <cstring>
#include <stdint.h>
#include <kos/thread.h>
using namespace std;
uint8_t buf[1024], buf2[1024];
int main(int argc, char *argv[]) {
FILE *fp;
uint64_t counter = 1;
size_t amt;
if(!(fp = fopen("/cd/1ST_READ.BIN", "rb"))) {
cout << "Cannot open 1ST_READ.BIN" << endl;
return -1;
}
amt = fread(buf, 1, 1024, fp);
if(amt != 1024) {
cout << "Error reading from file" << endl;
return -2;
}
fseek(fp, 0, SEEK_SET);
while(1) {
cout << "Test pass " << counter << endl;
amt = fread(buf2, 1, 1024, fp);
if(amt != 1024) {
cout << "Error reading file on pass " << counter << endl;
return -3;
}
if(memcmp(buf, buf2, 1024)) {
cout << "File contents don't match on pass " << counter << endl;
return -4;
}
fseek(fp, 0, SEEK_SET);
memset(buf2, 0, 1024);
++counter;
cout << "Test pass completed..." << endl << endl;
thd_sleep(500);
}
return 0;
}
-
- DC Developer
- Posts: 414
- Joined: Thu Aug 20, 2009 11:00 am
- Has thanked: 0
- Been thanked: 2 times
Re: Bug in C++ version of fread
The program opens all files in use and leaves them open until you explicitly delete the refs. Then functions are called that fseek/fread the open files. In the case we're talking about, a WAD file is being scanned for chunks. It reads the header okay, then failed to read the directory. When I got it to read the directory with a work-around, it then succeeded and failed on random chunks. Switching to fs_seek/fs_read "fixed" the problem.
I don't think a small snippet will exhibit the same issue. This seems like something that only pops up in a major size app. This one is 2M. If I was forced to guess, I'd guess some kind of issue with memory barriers or the like.
I don't think a small snippet will exhibit the same issue. This seems like something that only pops up in a major size app. This one is 2M. If I was forced to guess, I'd guess some kind of issue with memory barriers or the like.