tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] And now I'm having another problem with TCC


From: Emmanuel Deloget
Subject: Re: [Tinycc-devel] And now I'm having another problem with TCC
Date: Sun, 10 Jan 2016 22:30:39 +0100

Hello, 

On Sun, Jan 10, 2016 at 1:31 PM, Ben Hutchinson <address@hidden> wrote:
When I try to compile this:
#include <windows.h>
int _start(void){
    unsigned char MyArray[4];
    LPDWORD BytesWritten;
    HANDLE hFile;
    hFile=CreateFile("MyFile.dat",0xC0000000,0,0,2,0,0);
    MyArray[0]=1;
    MyArray[1]=2;
    MyArray[2]=3;
    MyArray[3]=4;
    WriteFile(hFile,MyArray,4,BytesWritten,0);

​Segfault here. BytesWritten is uninitialized, and point to nothing. ​

 
    CloseHandle(hFile);
}

I get a program that crashes when I run it. When I look at the dissassembly in OllyDbg, it turns out that what's happening is that in the WriteFile line of code, it's passing the value stored in BytesWritten, rather than its memory address, even though BytesWritten has been declared with LPDWORD, and even though LPDWORD has by defined (via typedef) as a pointer. When a pointer type variable is passed it should be passing the memory address, not the value stored there, but passing the value stored there is EXACTLY what's happening. The ONLY time that the value stored at a pointer should be passed is when you prefface that pointer with an asterisk. If I have a variable declared like this:
LPDWORD MyPtr
Every time I use that pointer, including passing it as a parameter in a function as just MyPtr, it SHOULD be passing the memory address, not the value stored there. The ONLY time that it should it should be using the value stored in a pointer-type variable there is if I am using an astrisk with it, like this:

​No. C knows nothing but by-value arguments ; it not not able to understand that the function needs to use the address of said argument instead of its value. The code you wrote is just plain wrong. If you try Visual C++ studio you'll get the same exact result (i.e. an access violation). Same if you use gcc, clang and any other compiler. You see, BytesWritten is a pointer that points to nothing (i.e. it probably points to either NULL or a memory region to which it has no read and/or write access). The right way to write it would be : 

DWORD BytesWritten = 0;
​...​
WriteFile(hFile, MyArray, sizeof(MyArray), &BytesWritten, 0);

 
*MyPtr
As you can see from the above code sample, my pointer-type variable is being passed in such a way that it should be passing the ADDRESS to the function, NOT passing the value, yet passing the value is exactly what's happening, as I can tell by running the program in a debugger.

This is a MAJOR glitch. It's what I call a "show stopper" glitch. It is significant enough that it will prevent you from from writing any decent program in TCC. I only wish the author of this software didn't stop updating it back in 2013. There's still quite a few bugs that are still NOT FIXED.


​​Weird. For unknown reasons, the tcc authors decided to create a compiler that does NOT correct semantic errors. Good thing to know, there is solution to overcome that sad situation: you just have to write correct code.​

​BR, 

-- Emmanuel Deloget​


reply via email to

[Prev in Thread] Current Thread [Next in Thread]