8 ms·
Your code should be taken out back, lined up against a wall, and machine-gunned
- chacha102 16y agoThen the bleeding corpse should be hung, drawn and quartered. Then burnt. Then the smouldering rubble should be jumped up and down on. By a hippo. By a hippo....
- JeanPierre 16y agoIt is a natural choice. 3,600 lb is more than the weight of any human in the world, and elephants can't jump.
- sliverstorm 16y agoWell, you'd at least hope so.
- afhof 16y agoWhy was he complaining about the stack pointer first? Isn't this line: st1 = (void *)malloc(5000) + 5000; completely invalid too? You can't (I think it's undefined) increment a void pointer.
- tptacek 16y agoIt may be undefined, but gcc accepts it without warning under -Wall and does exactly what you'd expect. Note that (a) casting malloc's return is evil, (b) casting malloc to void* is silly since it's declared returning void* already, and (c) casting malloc to void* so it can be received as a char* is also pretty goofy.
- NickPollard 16y agoThis might be a stupid question, but what are you meant to do if not cast malloc's return? My C is a bit rusty, but how else are you meant to allocate data of different types on the heap?
- scott_s 16y agoC allows implict casting of void* to any other kind of pointer. Hence, you never need to cast the return from a malloc if you're storing it in a pointer. C++, however, does not allow this. So, if for some reason you're calling malloc rather than new in C++, you need to cast.
- sanderjd 16y agoYou are not the poster who said "casting malloc's return is evil", so perhaps you don't agree with that statement, but I don't see how either of the points you make lead to the conclusion that it is "evil". You say that since void* is implicitly cast to all pointer types, you never need to explicitly cast it, but why would that make an explicit cast evil?
- tptacek 16y agoThere's nothing wrong with returning void. There is something wrong with casting void. First, as someone else already pointed out, it can lead to an LP64 bug if you miss the declaration for malloc(), since int and caddr_t are not necessarily the same thing. But the bigger problem is that casting to void* basically tells the compiler "stop all further type checking". That's not a big deal if the token "malloc" on that line never changes, but, in real codebases, allocators get changed all the time. If you change from foo_alloc() to bar_alloc(), the compiler will rightly freak out when you try to assign a foo* to a bar. But that (void) construction explicitly prevents that check from occurring, thus turning what should be a compile-time error into a runtime error.
- fintler 16y agoYep, it's basically undefined because a void type has no known size to do pointer arithmetic with.
- koenigdavidmj 16y agoGCC whines and moans, but treats it like a pointer to char.
- tptacek 16y agoGCC does not whine and moan about this, even if you ask for it to.
- koenigdavidmj 16y agoOh, right. (Forgot that my `gcc` is a shell script that adds a bunch of `-W` options.) You can ask for it with -Wpointer-arith.
- scdlbx 16y agoActually, since st1 is a char* and the sizeof char is 1, that is a valid declaration. It just probably isn't doing what the writer expects. It allocates 5000 bytes and then moves the pointer to point at the very last byte. edit: Though free()ing it would not work so well until you move the pointer back to the beginning of the 5000 bytes.
- azim 16y agoI think that's what the writer expected. On x86 the stack starts at a high address and grows downward to a lower address.
- mfukar 16y agoYes, arithmetic on void * is disallowed. However, gcc accepts it as an extension: http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC78 http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC78
- mattmight 16y agoOr, the code could be released under the CRAPL: http://matt.might.net/articles/crapl/ http://matt.might.net/articles/crapl/
- j_baker 16y agoI'm a little bit slow with the low level stuff. Could someone explain what's so horrific about this code?
- chipsy 16y agoC's function call mechanism requires internal maintenance of a call stack, which the programmer ordinarily never has to touch. The broken code author claims that he needs to manipulate said stack to write his Scheme interpreter. He does it wrong in a way that might pass the trivial "hello" test case under Linux, and blames Cygwin when it doesn't even pass that.
- ww520 16y agoFor one thing, he's using assembly of one type of machine and expects it to work on different type of machine. He confuses Linux as a machine abstraction and demands that Cygwin running on Windows to be the same. For the low level mess, he sets the stack frame pointer to a uninitialized heap-allocated buffer which contains God-know-what garbage. When main() returns, it pops its return address from the stack frame pointer, which contains garbage and crashes. The printf() might sometime work depending on how the compiler emits code to set up the stack frame. It just shows how dangerous some ignorant programmers can be.
- mrfu 16y agoFor further information http://insecure.org/stf/smashstack.html http://insecure.org/stf/smashstack.html is an interesting read.
- rntz 16y agoUh, actually, his original program (http://www.cygwin.com/ml/cygwin/2005-08/msg00542.html http://www.cygwin.com/ml/cygwin/2005-08/msg00542.html) did restore the stack pointer before returning from main. He "wanted to cut [the example's size] down for the sake of the mailing list", so he left that bit out.
- phaedrus 16y ago
- xpaulbettsx 16y agoWhile I always enjoy a good rant, I don't see why it had to be so vicious and insulting. Yeah, the guy's clearly pretty confused (if he wanted to do these kind of tricks, I'd try to use setjmp, fiddle with the structure, then longjmp to it), but there's no reason to be mean about it.
- ww520 16y agoI think it's because his indignant demand of the Cygwin team to fix this "bug" and make it work for him. Basically it's the attitude that I know my way is the right way, now fix the platform so that my program runs.
- xpaulbettsx 16y agoI don't see any demands in the original mail: http://www.cygwin.com/ml/cygwin/2005-08/msg00504.html http://www.cygwin.com/ml/cygwin/2005-08/msg00504.html He questions whether it "should be fixed" in Cygwin, a perfectly reasonable request, and he fully admits that he isn't sure whether it's a problem in Cygwin or not.
- ww520 16y ago"Should be fixed" is a passive aggressive way to demand it to be fixed. His reference to Linus doing the same thing in kernel (not) sounds very much like a smack response. Also he claims he knows what he's doing since he has done the same thing in DOS so when it's clear that he has no clue of what's going on just irrates people.
- tptacek 16y agoInstead of trying to walk "indignant demand" back to "passive aggressive way to demand" inch by inch, just concede that all the guy did was ask a question and make a case for why he thought Cygwin's behavior was broken. Nobody's saying Cygwin has to agree with him, but you've simply mischaracterized the start of the thread. My interpretation is that Korn was trying to be funny (and he succeeded), but as usual the peanut gallery ruined the joke.
- deleted 16y ago[deleted]
- boredguy8 16y agoBest part is in the follow-up discussion: > Do you actually realise that not all computer languages can > be implemented with a model of one stack? > Do you actually realize that there are far better ways to implement stacks for interpreters than dinking with esp? Ways that actually have a chance of working? I suggest you investigate those alternatives instead of trying to do something that cannot work, and then getting all pissy when people try to dissuade you from wasting your time. And don't make me break out my Turing Machine on you about that statement of yours.
- tptacek 16y agoStrong disagree. Korn's response is at least funny. The rest of the responses are petulant, somewhat ill-informed, and orthodoxical. The argument over whether C does or doesn't have a runtime is particularly tedious. You get the impression that these people would all be very, very upset at the Detours paper, except that Detours works so well they'd know a priori they'd lose the argumet. The original commenter --- despite his very fragile code --- is right: for what he wants to do, he needs to create temporary stacks. What he doesn't have is a mechanism to allocate and swap in and out of those stacks (he's hoping that switching stacks is a 2-liner that just relies on knowing the inline asm incantation for changing ESP). Regardless, it is a uniquely and flagrantly bad way to educate (both the questioner and the world who's reading the thread) to start from the premise that "you're not ready to build the thing you want to build" --- at least when "the thing" we're talking about is systems code. 90% of C programmers are not "ready" to build production C code that will get deployed in hostile environments, and as a result, in every web project we ever do, we have to embark on a tedious inventory of all the 3rd party stuff our clients use to find the horrible C ZIP library they accidentally included. No C coder is blameless. Unless I missed a whole leg of this thread (the part where they explained temp stacks and stack switching to this guy, who, in challenging the notion that C has a real runtime or the notion that the OS should [heh] be controlling ESP, is demonstrating more on-the-ground systems programming knowhow than several of the commenters), I think this is a remarkably douchey example of a programming help thread.
- ww520 16y ago
- ivenkys 16y agoI for one would pay good money to get a response like this. He points out the errors,the incorrect assumptions and offers pointers of what to do. The general standard of discourse on most mailing lists tend to be quite a bit lower than this.
- deleted 16y ago[deleted]
- nailer 16y agoThere's a great story about a heckler at a Windows NT conference: MS speaker: and we've got POSIX, and we've got POSIX ACLs, and we've got a Unix Subsystem and we've got Korn Shell, and it works just the same way as under Unix. Heckler: NO IT DOESN'T MS speaker: er, ok. It does work. Anyway... Heckler: NO IT DOESN'T WORK MS speaker: What doesn't work? Heckler: (rattles off differences in behavior and brokenness of ksh on NT) MS speaker: we'll we've had our top guys working on this ... Heckler: I'M DAVID KORN. Room erupts in laughter. Update: source: http://slashdot.org/articles/01/02/06/2030205.shtml http://slashdot.org/articles/01/02/06/2030205.shtml. See question 5.
- tokipin 16y agowe realllly need people like that at political speeches
- edge17 16y agoIs that the same Dave Korn that did Korn Shell?
- Confusion 16y agoIn case you haven't gathered from other responses in this thread or are only reading this in your 'threads' view: yes.
- thibaut_barrere 16y agoRecruiting tip: don't hire someone who is able to answer this way to someone, no matter how skilled.