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
Vanadium
Jan 8, 2005


Dude makes a final copy of that variable so it can be refered to in the closure that's supposed to run in another thread.

The null assignment would free memory insofar as it enables garbage collection of whatever was refered if it's the last reference, except I guess it doesn't in this case because the final variable is still around (which he can't null because it's final, durr). I think it's fair to call that a small bug that's not really related to thinking that reference assignment is deep copying

Edit: I would have bet that the threading stuff was introduced after the null assignment but the repos doesn't seem to go back far enough to tell. :shobon:

Vanadium fucked around with this message at 02:18 on Feb 17, 2012

Adbot
ADBOT LOVES YOU

Zombywuf
Mar 29, 2008

Look Around You posted:

I don't hate it but I like knowing whether or not calling a function will change it's arguments. That's the big thing about it for me.

This is why Ritchie granted us the power to name functions.

Scaevolus
Apr 16, 2007

Zombywuf posted:

This is why Ritchie granted us the power to name functions.
We don't need static typing, we have Hungarian notation.

Kelson
Jan 23, 2005

JewKiller 3000 posted:

Dereferencing a NULL pointer leads to the ubiquitous segmentation fault.

ctz posted:

No it doesn't. The behaviour is undefined.
CTZ is absolutely correct. Just to add a bit practically though, with x86 C/C++ dereferencing the NULL pointer is the same as dereferencing address 0x0. This is typically an invalid address, which causes JewKiller's seg fault. One can map memory to the NULL page in Windows however, which makes address 0 "valid."

Good exploitation vector.

raminasi
Jan 25, 2005

a last drink with no ice
I would love to see a C implementation where the undefined behavior was things like "changes your desktop wallpaper to a picture of dogs playing poker" instead of just crashing.

floWenoL
Oct 23, 2002

Kelson posted:

CTZ is absolutely correct. Just to add a bit practically though, with x86 C/C++ dereferencing the NULL pointer is the same as dereferencing address 0x0. This is typically an invalid address, which causes JewKiller's seg fault. One can map memory to the NULL page in Windows however, which makes address 0 "valid."

Good exploitation vector.

Even if the page containing 0x0 is invalid, dereferencing an invalid pointer can still do other things besides crash. Consider:

code:
// non-static
void MyClass::Foo() {
  std::cout << "blah";
}
((MyClass *)0)->Foo() will (probably) still work. On a more practical note, the function may end up calling other functions on member variables, so if that member variable offset is large enough, you could end up dereferencing a valid address.

So, yeah, undefined behavior is undefined.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

Vanadium posted:

Edit: I would have bet that the threading stuff was introduced after the null assignment but the repos doesn't seem to go back far enough to tell. :shobon:
I think the threading stuff was always in it. At least, that's my guess from there being a ResampleOpSingleThread in the experimental package.

Qwertycoatl
Dec 31, 2008

GrumpyDoctor posted:

I would love to see a C implementation where the undefined behavior was things like "changes your desktop wallpaper to a picture of dogs playing poker" instead of just crashing.

http://www.feross.org/gcc-ownage/

HORATIO HORNBLOWER
Sep 21, 2002

no ambition,
no talent,
no chance

GrumpyDoctor posted:

I would love to see a C implementation where the undefined behavior was things like "changes your desktop wallpaper to a picture of dogs playing poker" instead of just crashing.

Still waiting on the canonical "demons fly out of your nose" implementation.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Ah yes, the Armed Response Technologies Deathstation 9000.

Vanadium
Jan 8, 2005

GrumpyDoctor posted:

I would love to see a C implementation where the undefined behavior was things like "changes your desktop wallpaper to a picture of dogs playing poker" instead of just crashing.

I'd love to see a pedagogical C/C++ implementation that goes to great lengths of runtime support and everything to give precise diagnostics for every bit of undefined (or implementation-defined, where the standard gives the implementation room to crash) behavior. It'd be great for arguing about dumb language-lawyery bullshit on IRC. :shobon:

hobbesmaster
Jan 28, 2008

Was using 'eor' as your mnemonic for 'exclusive or' as retarded in 1983 as it seems now?

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

Vanadium posted:

I'd love to see a pedagogical C/C++ implementation that goes to great lengths of runtime support and everything to give precise diagnostics for every bit of undefined (or implementation-defined, where the standard gives the implementation room to crash) behavior. It'd be great for arguing about dumb language-lawyery bullshit on IRC. :shobon:

Let's goon project this poo poo

We have a couple of people itt that work or have worked on clang, right

mjau
Aug 8, 2008

hobbesmaster posted:

Was using 'eor' as your mnemonic for 'exclusive or' as retarded in 1983 as it seems now?

I don't know about other processors, but the official name of the exclusive or op on the 6502 family was "eor". I learned to code on the C64 and got used to "eor" in the assemblers there, so when I first saw the "xor" spelling, I thought that was weird. (I prefer it now though.)

xf86enodev
Mar 27, 2010

dis catte!

hobbesmaster posted:

Was using 'eor' as your mnemonic for 'exclusive or' as retarded in 1983 as it seems now?

xor just like xml are right there in the same league of marketing bullshit "X sounds so far-out, man!"

Xtreme programming anyone?

PrBacterio
Jul 19, 2000

Vanadium posted:

I'd love to see a pedagogical C/C++ implementation that goes to great lengths of runtime support and everything to give precise diagnostics for every bit of undefined (or implementation-defined, where the standard gives the implementation room to crash) behavior. It'd be great for arguing about dumb language-lawyery bullshit on IRC. :shobon:
I'm fairly sure this program exists and is called "valgrind" though?

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

PrBacterio posted:

I'm fairly sure this program exists and is called "valgrind" though?

Does that blow up on things like assigning a size_t to an int?

MrMoo
Sep 14, 2000

ICC would probably be sufficient for that, it whines like crazy.

PrBacterio
Jul 19, 2000

Jabor posted:

Does that blow up on things like assigning a size_t to an int?
But assigning a size_t to an int doesn't result in undefined or implementation-dependant behaviour (at least as long as the value of the size_t will also fit in an int), as it's just a standard integer typecast - as size_t is the same type as exactly one out of unsigned short int, unsigned int or unsigned long int. Probably there exists some tool for valgrind to make it blow up if the conversion fails due to overflow, though.

shrughes
Oct 11, 2008

(call/cc call/cc)

PrBacterio posted:

Probably there exists some tool for valgrind to make it blow up if the conversion fails due to overflow, though.

This is impossible, there's no information at runtime about whether a value's supposed to be signed or unsigned, or that a truncation conversion is happening.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
Also there will never be a tool for valgrind though that flags if (size > size+1) {...} as dead code and gives a warning explaining why

Opinion Haver
Apr 9, 2007

Otto Skorzeny posted:

Also there will never be a tool for valgrind though that flags if (size > size+1) {...} as dead code and gives a warning explaining why

Because it's not dead if size = INT_MAX? :v:

pseudorandom name
May 6, 2007

Overflow on signed ints is undefined, so the compiler is free to assume that condition is always false.

hirvox
Sep 8, 2009

HORATIO HORNBLOWER posted:

Still waiting on the canonical "demons fly out of your nose" implementation.
You'll probably find one in Charles Stross' Laundry novels.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

yaoi prophet posted:

Because it's not dead if size = INT_MAX? :v:

Nope

"LLVM's project blog post about this posted:

Lets look at an example. Consider this carefully written C code:

code:
void process_something(int size) {
  // Catch integer overflow.
  if (size > size+1)
    abort();
  ...
  // Error checking from this code elided.
  char *string = malloc(size+1);
  read(fd, string, size);
  string[size] = 0;
  do_something(string);
  free(string);
}
This code is checking to make sure that the malloc is big enough to hold the data read from the file (because a nul terminator byte needs to be added), bailing out if an integer overflow error occurs. However, this is exactly the example we gave before in which the compiler is allowed to (validly) optimize out the check. This means that it is perfectly possible for the compiler to turn this into:

code:
void process_something(int *data, int size) {
  char *string = malloc(size+1);
  read(fd, string, size);
  string[size] = 0;
  do_something(string);
  free(string);
}


Nb. if you write a simple loop to test this you'll find that real live compilers do indeed perform this optimization.

taqueso
Mar 8, 2004


:911:
:wookie: :thermidor: :wookie:
:dehumanize:

:pirate::hf::tinfoil:

Do people really use if(size > size + 1) instead of if(size == INT_MAX)? The second seems clearer and avoids the optimization issue. Or is this just for the sake of the example?

pseudorandom name
May 6, 2007

The usual (wrong) example is if (p + SOME_SIZE < p).

hobbesmaster
Jan 28, 2008

xf86enodev posted:

xor just like xml are right there in the same league of marketing bullshit "X sounds so far-out, man!"

Xtreme programming anyone?

Sometimes its hard to tell if people are joking, but I've only ever seen XOR in my logic textbooks... it really doesn't matter but I have this twitch whenever I use ARM assembly.

feedmegin
Jul 30, 2008

hobbesmaster posted:

Sometimes its hard to tell if people are joking, but I've only ever seen XOR in my logic textbooks... it really doesn't matter but I have this twitch whenever I use ARM assembly.

ARM is a British-designed ISA. We don't cotton to xtreme letterz as much as you lot :britain:

Suspicious Dish
Sep 24, 2011

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

Otto Skorzeny posted:

Nope

C only goes fast because compilers are allowed to write code that doesn't do what you wanted.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
In the same sense that C is considered a "simple" language because nobody knows how compilers actually handle it.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Incidentally, clang already does have a -fcatch-undefined-behavior flag, and John Regehr (equivocation incoming) has had at least one student at some point in the past working on extending it. So some of this is already done.

The really tricky part of catching all undefined behavior would be not integer overflow but things that rely on the effective type of an object, like the union type-punning rules and the aliasing rules. You'd need some sort of crazy side-table to avoid breaking ABI.

(and yes, I am employed to work on clang full-time)

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

rjmccall posted:

like the union type-punning rules

I thought the rules were 'just' that this wasn't allowed, eg. a read of type A from union {A, B} foo is only allowed if the last write to foo was of type A?

Scaevolus
Apr 16, 2007

Otto Skorzeny posted:

I thought the rules were 'just' that this wasn't allowed, eg. a read of type A from union {A, B} foo is only allowed if the last write to foo was of type A?
When how something would be implemented isn't obviously portable, the C/C++ standards tend to err on the side of "undefined"/"implementation dependent" rather than "illegal".

iirc, C99 doesn't even guarantee that integers are represented by two's complement.

Scaevolus fucked around with this message at 21:07 on Feb 19, 2012

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

Scaevolus posted:

iirc, C99 doesn't even guarantee that integers are represented by two's complement.

1's complement and sign-magnitude are the other two options.

Also it doesn't guarantee that all possible bit patterns are actually valid.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Otto Skorzeny posted:

I thought the rules were 'just' that this wasn't allowed, eg. a read of type A from union {A, B} foo is only allowed if the last write to foo was of type A?

Pretty much, although there's an exception (in both C and C++) about structs with a common prefix.

Haud
Dec 6, 2007

World's Worst Interview
code:
if(x == 0)
   //Do Nothing
else
   DoSomething();
I took over development of a program, and this guy's work is full of stuff like this. Not even using return or anything, just comments that say //Do Nothing. I'm starting to wonder if he even knows that > and < exist.

TheresaJayne
Jul 1, 2011

Haud posted:

code:
if(x == 0)
   //Do Nothing
else
   DoSomething();
I took over development of a program, and this guy's work is full of stuff like this. Not even using return or anything, just comments that say //Do Nothing. I'm starting to wonder if he even knows that > and < exist.

I see that a lot, i think because code validators like PMD (in java) complain about
code:
if(variable != null) { 
do this
}
so people do stuff like this

code:
if(variable == null) {
  //do nothing
} else {
  //dosomething
}

feedmegin
Jul 30, 2008

Jabor posted:

1's complement and sign-magnitude are the other two options.

Also it doesn't guarantee that all possible bit patterns are actually valid.

There were actual real computers using non-twos-complement maths around when C became a Thing -

http://en.wikipedia.org/wiki/CDC_6000_series
http://en.wikipedia.org/wiki/IBM_7090

for example. There might even be some still knocking around with modern C compilers, for all I know; it makes sense the C people wouldn't want to rule out the possibility.

Adbot
ADBOT LOVES YOU

darthbob88
Oct 13, 2011

YOSPOS
Is it cool to talk about terrible ideas here? Because a colleague/relation of mine had a brilliant idea to create a StackOverflow-type site, where the users post a poll with their question, "Which answer is right?". Other folks vote on that poll and by the Wisdom of the Group truth shall emerge and the best answer will win out, and those who voted for the correct answer get karma/rep for being right or just agreeing with the most people, which gives them more weight in any further polls they vote on. It would combine all the wisdom of Stack Overflow with the democracy of Reddit, in a final product that would, he assures me, make us zillions of dollars if I just do the backend work for him. Additional problems include the obvious fact that many users won't have a good idea as to what the truth is, and the old Put down the chocolate covered banana situations. I do like that phrase.

Incidentally, this same dude thought the Unix Millennium bug could be solved by replacing the ints everywhere with doubles, because they have a larger range. :eng99:

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