Nicolás Brailovsky


A modern blog

Archive for the ‘WTF’ Category

Just WTF

author Posted by: nico on date Jun 8th, 2010 | filed Filed under: WTF

Who the hell can create a monster like this one?

  1. CREATE PROC shutdown10
  2. AS
  3. EXEC xp_cmdshell ‘net send /domain:SQL_USERS ”SQL Server
  4. shutting down in 10 minutes. No more connections
  5. allowed.’, no_output
  6. EXEC xp_cmdshell ‘net pause sqlserver’
  7. WAITFOR DELAY ‘00:05:00′
  8. EXEC xp_cmdshell ‘net send /domain: SQL_USERS ”SQL Server
  9. shutting down in 5 minutes.’, no_output
  10. WAITFOR DELAY ‘00:04:00′
  11. EXEC xp_cmdshell ‘net send /domain:SQL_USERS ”SQL Server
  12. shutting down in 1 minute. Log off now.’, no_output
  13. WAITFOR DELAY ‘00:01:00′
  14. EXEC xp_cmdshell ‘net stop sqlserver’, no_output

Dealing with Office on Linux

author Posted by: nico on date May 18th, 2010 | filed Filed under: Funny, Grumpy

Seen @ bash.org:

< adamkuj> are there any good open source tools for working with Access DB’s (mdb files)?
<@Dopey> rm
Comment: #evilgeeks

True, so true… It’s been said a thousand times, but I kinda like ranting… like it or not Office files are a majority out there. If you work with Linux, using it in a real enterprise environment, sooner or later you’ll run into someone using a propietary Office format. You’ll also run into someone who doesn’t know that you need OOO to open those weird odf files. You may need to produce an adecuately formated document sometime, and LaTeX may not be an option.

Deal with it. Install a VM, or Wine, and a copy of MS Office. Resistance is futile.

Random WTFs

author Posted by: nico on date May 10th, 2010 | filed Filed under: Grumpy, WTF

Random WTF 1

Note: Use dapper instead of edgy to use Ubuntu Dapper

Thank you captian obvious.

Random WTF 2

I’ve been working with a big-co supplied ACS server, but it’s IE only. WTF!? Aren’t ACS all XMLy so they can work everywhere? I hate you all.

Ubuntu: Sound still FUBAR’d

author Posted by: nico on date May 4th, 2010 | filed Filed under: Grumpy, Linux, WTF
Remember my problems with dual screen support in Ubuntu? Well, I still love bashing Ubuntu, and the sound system in Linux is certainly a topic to rant a lot. Making the sound work fine in Ubuntu is an odyssey in pain and frustration, unless it works fine out of the box. And even if it does, it may still have it’s kirks. Lots of them.

In my case the sound starts in mute. I know it’s a problem with pulse (which is a WTF in itself) and alsa, I don’t really care what’s the problem though, I just want to play my mp3s collection without having to carefully turn the knobs up to eleven in alsamixer.

After trying a lot of the “solutions” found on the internets I’ve decided the best thing to do, short of switching back to windows me, is adding the following to my “fix_ubuntu_fuckups.sh” start script, which already contains my dual-screen pseudofix:

  1. amixer -c0 — sset Master playback -0dB unmute
  2. amixer -c0 — sset Headphone playback 0dB unmute
  3. amixer -c0 — sset Front playback 0dB unmute
  4. amixer -c0 — sset PCM playback -16dB unmute

This sets alsamixer to normal volume levels. As for the real fix, I’ll wait till the next Ubuntu version. I wonder which sound subsystem will they chose next time.

Company policy

author Posted by: nico on date Apr 19th, 2010 | filed Filed under: Funny

I liked this one:

Great for a 404.

Operator sizeof (AKA Reading Berkeley’s FM, take II)

author Posted by: nico on date Mar 29th, 2010 | filed Filed under: C++, Grumpy, Programming, WTF

Last time I told you about an evil snipet I found on Oracle Berkeley DB’s manual:

  1.  skey->size = sizeof((struct student_record *)pdata->data)->last_name;
And we concluded it’s trying to… well, dereference a number. And yet it compiles. What the hell is going on there?

The answer here is in the subtleties of the sizeof operator. That’s right, operator, not function. Plus is an operator. Less is an operator. * is a (unary) operator. sizeof is a unary operator too. The relevance of this is that operators can behave in more bizzare ways than functions do. In this case there’s a difference between this two lines:

  1.  MyClass x;
  2.   int a = sizeof(MyClass);
  3.   int b = sizeof(x);

A very subtle difference. Can you spot it? a and b will have the exact same value, rest assured. The difference is in the operator itself: sizeof MUST have parenthesis when applied to a type name, yet parenthesis are optional when applied to an instance of a datatype, so this code is legal:

  1.  MyClass x;
  2.   int a = sizeof(MyClass);
  3.   int b = sizeof x;

Oh, wait, the fun doesn’t stop there: sizeof also has bizarre precedence order, meaning it won’t get applied as you expect it. So, this is valid too:

  1.  struct MyClass { int y; } x;
  2.   int b = sizeof x->y;

Can you see where we are going? Knowing that sizeof will be applied last lets you write something like this too:

  1.  void *ptr = …
  2.   int b = sizeof((X*)ptr)->y;

Which means nothing else than “store in b the size of member y in struct X. It should be easy to see why BDB’s example does compile, and why did I spend half an hour trying to understand the reason it compiled fine.

By using some more casts and a clever arangement of parenthesis you can come up with a great job security device.

Reading Berkeley’s FM

author Posted by: nico on date Mar 26th, 2010 | filed Filed under: C++, Grumpy, Programming, WTF

I got this from Oracle Berkely DB’s FM:

  1.  skey->size = sizeof((struct student_record *)pdata->data)->last_name;

Take a good look at that pice of code:

  1.  a_number = sizeof((T*)pdata->data)->last_name;

Again:

  1.  a_number = sizeof(Whatever)->field;

Wait a minute. typeof(sizeof(x)) == const unsigned int. Right? So, again:

  1.  a_number = 42->field;

There’s no way that first line can compile. Go and check it (in the example, not the last line please). I’ll wait. Done? Yeap, I was surprised to, it does indeed compile. Mi first reaction towards this discovery went something like this:

What is going on there? It took me a while to figure out how evil Berkely ’s manual can be. The answer next time.

You know you’re a geek…

author Posted by: nico on date Mar 23rd, 2010 | filed Filed under: WTF

… when you try to log in to your homebanking account using admin:admin (*) (**)

(*) Alt take: a geek with weak passwords, yeah. My pin is 1234 and I’ll never change it.
(**) Replace homebanking with gmail, linkedin and $LATEST_NETWORKING_FAD and you’re most likely mental. (***)
(***) Replace mental with (*) to obtain a Moebious post.

/Delirious posting mode, deactivate!

C++: Composite objects and throwing constructors

author Posted by: nico on date Feb 25th, 2010 | filed Filed under: C++, WTF

Check out the following code:

  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct Foo {
  6.         struct Foobar{ Foobar(){ throw "foobar’d"; } };
  7.         Foobar baz;
  8.  
  9.         Foo() try : baz() {
  10.                 cout << "Ctr 1\n";
  11.         }catch(){
  12.                 cout << "Ctr 2\n";
  13.         }
  14. };
  15.  
  16. int main() {
  17.         Foo bar;
  18.         cout << "End!\n";
  19.         return 0;
  20. }
  21.  

Nice, isn’t it? Without using a compiler answer the following questions:

  • Does it compile?
  • If it does, does it abort or does it return 0?
  • What does it print?

Did you think about it? Come on, I’ll wait… ready? OK, lets go.

First we should think about something else: what the fuck is that thing? I would surely be horrified if I found something like that was lurking in one of my projects’ code. It’s hideous. And it’s called a “Constructor function-try-block” (yes, answering the first question, it does compile and it indeed is valid C++ code).

A constructor function-try-block exists only to catch exceptions thrown while constructing a composite object. This may tempt us to answer the second question: it should return 0, as we’re catching Foobar’s exception on Foo’s ctr. But that’s not how these things work , otherwise this would have been a very boring entry: the program aborts.

To understand why does this program abort you should think what does it mean to have a composite object; there is no meaning in having a Foo without a Foobar. baz, of type Foobar is a part of Foo, just like a head is a part of a person and there’s no person without a head (though many act as if they didn’t have a working head, but that is a topic for another day).

Now, what does it mean to throw (regardless wheter this is a good or a bad practice) in a constructor? It is like saying “There’s been a horrible error and I have no idea how to recover. Man, I give up”. Throwing in a ctr means there’s no point in trying to fully construct that object: it leaves a half-built thing. What can you do with that half object? Nothing, throw it away.

Now that we know what does it mean to throw in a constructor we can answer why does the program abort: there is no point in building a composite object when one of its constituting parts can NOT be constructed, thus it must throw as well.

The last point, why do we have function try blocks if we can’t catch exceptions? Well… you can’t catch it but you may rethrow a different one, or use the constructor to clean up the mess you would otherwise leave behind. Or you could write a snippet of code, post it on your blog and confuse a whole lot of people (all 3 of them reading this block. Hi grandma).

Oh, we had a third question, but you should be able to answer that one yourself.

PS: You may get a better explanation at GotW #66: Constructor Failures

Write in C

author Posted by: nico on date Oct 15th, 2009 | filed Filed under: C++, WTF

From http://www.youtube.com/watch?v=J5LNTTGDKYo&feature=related
Write in C

When I find my code in tons of trouble,
Friends and colleagues come to me,
Speaking words of wisdom:
“Write in C.”

As the deadline fast approaches,
And bugs are all that I can see,
Somewhere, someone whispers:
“Write in C.”

Write in C, write in C,
Write in C, oh, write in C.
LISP is dead and buried,
Write in C.

I used to write a lot of FORTRAN,
For science it worked flawlessly.
Try using it for graphics!
Write in C.

If you’ve just spent nearly 30 hours
Debugging some assembly,
Soon you will be glad to
Write in C.

Write in C, write in C,
Write in C, yeah, write in C.
BASIC is for wimps.
Write in C.

Write in C, write in C,
Write in C, oh, write in C.
Pascal won’t quite cut it.
Write in C.

Write in C, write in C,
Write in C, yeah, write in C.
Don’t even mention COBOL.
Write in C.

And when the screen is fuzzy,
And the editor is bugging me.
I’m sick of ones and zeros,
Write in C.

A thousand people swear that T.P.
Seven is the one for me.
I hate the word PROCEDURE,
Write in C.

Write in C, write in C,
Write in C, yeah, write in C.
PL1 is ’80s,
Write in C.

Write in C, write in C,
Write in C, yeah, write in C.
The government loves ADA,
Write in C.

Write in C, write in C,
Write in C, yeah, write in C.
Java’s not quite there yet,
Write in C.