www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - winsamp.d and -O

reply "=?ISO-8859-1?B?Lg==?=" <bitworld qq.com> writes:
	charset="ISO-8859-1"
Content-Transfer-Encoding: base64

VGhlIHdpbnNhbXAuZCBpcyBkaXN0cmlidXRlZCB3aXRoIERNRC4gIEl0J3MgT0sgd2hlbiBi
ZWluZyBidWlsdCBsaWtlIHRoaXM6DQogICBkbWQgd2luc2FtcCBnZGkzMi5saWIgd2luc2Ft
cC5kZWYNCg0KSG93ZXZlciwgcnVubmluZyB0aGlzOg0KICAgZG1kIC1PIHdpbnNhbXAgZ2Rp
MzIubGliIHdpbnNhbXAuZGVmDQp3aWxsIGlzc3VlIGEgZXJyb3IgbWVzc2FnZToNCiAgIHdp
bnNhbXAuZCgzNSk6IEVycm9yOiBudWxsIGRlcmVmZXJlbmNlIGluIGZ1bmN0aW9uIFdpbmRv
d1Byb2MNCg0KQ2hlY2sgaXQsIHlvdSB3aWxsIGdldCB0aGlzOg0KKihjYXN0KGludCopIG51
bGwpID0gNjY2Ow0KDQpCZWZvcmUgZmlyaW5nIGEgYnVnLCBJIHdhbnQgdG8gbWFrZSBzdXJl
IGFib3V0IGl0Lg0KDQpUaGFua3Mu
Jun 14 2011
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 14.06.2011 16:18, . wrote:
 The winsamp.d is distributed with DMD.  It's OK when being built like 
 this:
    dmd winsamp gdi32.lib winsamp.def

 However, running this:
    dmd -O winsamp gdi32.lib winsamp.def
 will issue a error message:
    winsamp.d(35): Error: null dereference in function WindowProc

 Check it, you will get this:
 *(cast(int*) null) = 666;

 Before firing a bug, I want to make sure about it.
Seems like a bad joke, so file a bug. In all honesty it's not a good way to greet beginners. -- Dmitry Olshansky
Jun 14 2011
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
LOL how the hell did that get in there? I remember I was updating
these samples some time ago.. maybe it was my fault. I am completely
sorry if this was my fault.
Jun 14 2011
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 6/15/11, Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:
 LOL how the hell did that get in there? I remember I was updating
 these samples some time ago.. maybe it was my fault. I am completely
 sorry if this was my fault.
Oh wait, it's not my fault! It's a demonstration, see: case IDC_BTNDONTCLICK: if (HIWORD(wParam) == BN_CLICKED) { MessageBoxA(hWnd, "You've been warned...", "Prepare to GP fault", MB_OK | MB_ICONEXCLAMATION); *(cast(int*) null) = 666; } The difference is, DMD can catch this at compile time with optimizations turned on. I don't know why. Anyway, the example is supposed to show what happens on a wrong pointer dereference.
Jun 14 2011
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 15.06.2011 2:03, Andrej Mitrovic wrote:
 On 6/15/11, Andrej Mitrovic<andrej.mitrovich gmail.com>  wrote:
 LOL how the hell did that get in there? I remember I was updating
 these samples some time ago.. maybe it was my fault. I am completely
 sorry if this was my fault.
Oh wait, it's not my fault! It's a demonstration, see: case IDC_BTNDONTCLICK: if (HIWORD(wParam) == BN_CLICKED) { MessageBoxA(hWnd, "You've been warned...", "Prepare to GP fault", MB_OK | MB_ICONEXCLAMATION); *(cast(int*) null) = 666; } The difference is, DMD can catch this at compile time with optimizations turned on. I don't know why. Anyway, the example is supposed to show what happens on a wrong pointer dereference.
I think generic 'windows sample' is _not_ about null pointer dereference (though arguably one can consider Windows as a whole an example on GP fault and null pointer dereference in particular). -- Dmitry Olshansky
Jun 14 2011
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 6/15/11, Dmitry Olshansky <dmitry.olsh gmail.com> wrote:
 I think generic 'windows sample' is _not_ about null pointer dereference
Actually there's another problem. In WinMain, this line: catch (Exception e) // catch any uncaught exceptions should be: catch (Throwable e) // catch any uncaught exceptions That way when a null is dereferenced you'll have the exception caught and get a nice error message. That's what this sample was supposed to demonstrate. I'll have it updated and make a pull.
 (though arguably one can consider Windows as a whole an example on GP
 fault and null pointer dereference in particular).
I don't follow.
Jun 14 2011
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 15.06.2011 2:23, Andrej Mitrovic wrote:
 On 6/15/11, Dmitry Olshansky<dmitry.olsh gmail.com>  wrote:
 I think generic 'windows sample' is _not_ about null pointer dereference
Actually there's another problem. In WinMain, this line: catch (Exception e) // catch any uncaught exceptions should be: catch (Throwable e) // catch any uncaught exceptions That way when a null is dereferenced you'll have the exception caught and get a nice error message. That's what this sample was supposed to demonstrate. I'll have it updated and make a pull.
Makes sense. I'd better stop arguing this cause, and read more on that awesome DIP11 ;)
 (though arguably one can consider Windows as a whole an example on GP
 fault and null pointer dereference in particular).
I don't follow.
nvm, a pun that apparently out of date. -- Dmitry Olshansky
Jun 14 2011
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
If you're feeling adventurous you could always put up a Linux GUI
example. Xlib would be the standard lib, afaik?
Jun 14 2011
parent reply Robert Clipsham <robert octarineparrot.com> writes:
On 14/06/2011 23:50, Andrej Mitrovic wrote:
 If you're feeling adventurous you could always put up a Linux GUI
 example. Xlib would be the standard lib, afaik?
I believe xcb is what's recommended, it's faster and more lightweight. This said, you'd have to be insane to work with X directly, better off using QtD or GtkD. -- Robert http://octarineparrot.com/
Jun 14 2011
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Well, turns out there's no sign of the samples folder on github. :/
Jun 14 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
To make the example compile with optimizations, you have to trick the
compiler so it can't determine whether it's a null dereference at
compile-time. Change the code to this:

int* p;
extern (Windows)
int WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_COMMAND:
        {
            switch (LOWORD(wParam))
            {
                case IDC_BTNCLICK:

                    if (HIWORD(wParam) == BN_CLICKED)
                        MessageBoxA(hWnd, "Hello, world!", "Greeting",
                                    MB_OK | MB_ICONINFORMATION);

                    break;

                case IDC_BTNDONTCLICK:

                    if (HIWORD(wParam) == BN_CLICKED)
                    {
                        MessageBoxA(hWnd, "You've been warned...",
"Prepare to GP fault",
                                    MB_OK | MB_ICONEXCLAMATION);
                        *p = 1;
                    }

... more code ...

And then it will compile and throw at runtime when u press the dontclick button.
Jun 14 2011