HeapAlloc vs. malloc. How does HeapAlloc work?

Open forum for end-user questions about Wine. Before asking questions, check out the Wiki as a first step.
Forum Rules
Locked
Petr Svoboda

HeapAlloc vs. malloc. How does HeapAlloc work?

Post by Petr Svoboda »

Hi,
I am little bit interested in Wine source code. There is one thing I don't understand. How does HeapAlloc() work? Is it the same behavior as Winapi HeapAlloc()? If so then I have to check NULL value after each calling of this function, don't I?

I have found this code in Wine 0.9.57:

In dlls/dbghelp/stabs.c function 'static int stabs_new_include(const char* file, unsigned long val)' line 163:

        if (!include_defs)
            include_defs = HeapAlloc(GetProcessHeap(), 0,
                                     sizeof(include_defs[0]) * num_alloc_include_def);
        else
            include_defs = HeapReAlloc(GetProcessHeap(), 0, include_defs,
                                       sizeof(include_defs[0]) * num_alloc_include_def);
        memset(include_defs + num_include_def, 0, sizeof(include_defs[0]) * 256);
    }
    include_defs[num_include_def].name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(file) + 1), file);
    include_defs[num_include_def].value = val;
    include_defs[num_include_def].vector = NULL;
    include_defs[num_include_def].nrofentries = 0;

Nobody is checking NULL value of include_defs and later there is strcpy() with HeapAlloc as parameter.

In dlls/winspool.drv/info.c function 'static LPDEVMODEA DEVMODEdupWtoA(HANDLE heap, const DEVMODEW *dmW)' line 1709

     if (pName) {
         len = MultiByteToWideChar(CP_ACP, 0, pName, -1, NULL, 0);
        nameW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
         MultiByteToWideChar(CP_ACP, 0, pName, -1, nameW, len);
     }

Again nobody is checking the nameW value. Maybe it is checked inside 'MultiByteToWideChar()' but this function doesn't return any value in this case.

There are places where value returned by HeapAlloc() is checked:

In dlls/wininet/internet.h 'static inline LPWSTR WININET_strdupW( LPCWSTR str )' line 82:

    LPWSTR ret = HeapAlloc( GetProcessHeap(), 0, (strlenW(str) + 1)*sizeof(WCHAR) );
    if (ret) strcpyW( ret, str );
    return ret;

Could somebody explain me the behavior of Wine HeapAlloc() and its family?

Thank you,
cunha
Ove Kaaven

HeapAlloc vs. malloc. How does HeapAlloc work?

Post by Ove Kaaven »

Petr Svoboda skrev:
I am little bit interested in Wine source code. There is one thing I don't understand. How does HeapAlloc() work? Is it the same behavior as Winapi HeapAlloc()?
Yes.
If so then I have to check NULL value after each calling of this function, don't I?
Yes. You have to do that with malloc() too, so why "vs malloc"?
Nobody is checking NULL value of include_defs and later there is strcpy() with HeapAlloc as parameter.
Well, it's the age-old hazards of programming in C: people get lazy
about error checking, which has caused various problems in all kinds of
software. (In this case, perhaps thinking that if you're out of memory,
then you're in so much trouble anyway that the null-pointer-dereference
exception to follow isn't such a big deal...)
Locked