Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#52 fopen always fails on custom devoptab devices

closed
nobody
devkitPPC (14)
5
2012-10-09
2008-03-27
Sven Peter
No

I think I found a bug in the fopen functions of devkitPPC when it is used with a custom devoptab device.
The main problem is that the Wii's memory starts at 0x80000000 which will always become negative when casted to an integer and make one check in fopen fail.

devkitPPC's _open_r function allocates the handle in the following way (this code is taken from buildscripts/dkppc/patches/newlib-1.15.0.patch line 5273 in the CVS):
handle = (handle *)malloc( sizeof(handle) + devoptab_list[dev]->structSize );
The memory address of the handle will therefore be somewhere above 0x80000000. This handle is then casted to an int and passed to the custom devoptab _open_r function and later returned to fopen.

newlib's fopen functions does the call to _open_r in the following way (taken from src/newlib/stdio/fopen.c)
if ((f = _open_r (ptr, file, oflags, 0666)) < 0)
{
__sfp_lock_acquire ();
fp->_flags = 0; / release /

ifndef SINGLE_THREAD

  __lock_close_recursive (fp->_lock);

endif

  __sfp_lock_release ();
  return NULL;
}

Because the return value of _open_r will always be negative as it's a memory address casted to int fopen will always fail no matter what the function specified in the devoptab struct does.
A possible fix is to just make fopen check if f is -1, which is the only error value returned by _open_r.

I attached a text file with both full functions and two comments to demonstrate where the error occurs.

Discussion

  • Sven Peter
    Sven Peter
    2008-03-29

    Logged In: YES
    user_id=2043874
    Originator: YES

    I can defiantly confirm this bug now as my libfat port works only when I changes those lined mention in the initial bug report. There are two similar bugs in the _read and _write functions. (libgloss/libsysbase/read.c and write.c):
    if (fileDesc < 3) {
    needs to be changed to
    if (fileDesc > -1 && fileDesc < 3) {
    to fix a crash when using those functions on a custom devoptab device.

     
  • Sven Peter
    Sven Peter
    2008-06-02

    Logged In: YES
    user_id=2043874
    Originator: YES

    fixed in latest cvs / devkitPPC r15