Most active commenters
  • corndoge(7)
  • MaulingMonkey(4)

←back to thread

387 points pedro84 | 47 comments | | HN request time: 2.078s | source | bottom
1. Animats ◴[] No.14860964[source]
C's lack of array size info strikes again:

    memcpy(current_wmm_ie, ie->data, ie->len);
where "ie" points to data obtained from the net.
replies(2): >>14861129 #>>14861284 #
2. revelation ◴[] No.14861129[source]
C's lack of arrays strikes again. They are essentially syntactic sugar.
replies(1): >>14861235 #
3. frlnBorg ◴[] No.14861235[source]
What do you mean by C not having arrays?
replies(5): >>14861254 #>>14861256 #>>14861257 #>>14861262 #>>14861281 #
4. JustSomeNobody ◴[] No.14861254{3}[source]
Probably referring to the fact that they are simply pointers into contiguous memory.
replies(1): >>14862377 #
5. ◴[] No.14861256{3}[source]
6. CobrastanJorji ◴[] No.14861257{3}[source]
I assume they mean that C's support for "arrays" is essentially just C's support for pointer arithmetic plus a mapping of a[b] to *(a+b).
replies(2): >>14861354 #>>14862014 #
7. kinkrtyavimoodh ◴[] No.14861262{3}[source]
It's syntactic sugar in the sense that arr[i] is just shorthand for *(arr+i)

There's no abstraction or 'concept' of arrays there. You are literally just telling the compiler to take a certain pointer and move i steps ahead.

replies(1): >>14861315 #
8. pmontra ◴[] No.14861281{3}[source]
They are a pointer and an offset with no validation of bounds. But that's OK: C is little more than a high level assembly.
replies(1): >>14861607 #
9. corndoge ◴[] No.14861284[source]
Programmer's mistake for not validating data, not the fault of C language mechanics. Yes it would be easier if <hll features>, still gotta be careful. I've made plenty of these mistakes but never blamed the language.
replies(5): >>14861427 #>>14861944 #>>14861961 #>>14866096 #>>14871609 #
10. corndoge ◴[] No.14861315{4}[source]
Isn't that the definition of an array? Chunk of contiguous memory plus a notion of how to subdivide it into equal parts?
replies(2): >>14861327 #>>14861945 #
11. kuschku ◴[] No.14861327{5}[source]
Yes, exactly. A chunk of memory. A chunk has an end.

But "arrays" in C aren't a chunk of memory, just the info where it starts and how large elements are.

replies(1): >>14862551 #
12. Kubuxu ◴[] No.14861354{4}[source]
You can even swap it a[b] equivalent to b[a].
13. orf ◴[] No.14861427[source]
If a manufacturer makes and sells a gun that keeps going off in people's holsters and shooting people in the foot, the answer is not to say "it's the user's fault for not using it properly. I've shot myself in the foot hundreds of times and I don't blame the manufacturer".

Or something. That analogy sounded better in my head than written down. The point is that IMO the blame lies squarely with the C language: it's a language that's used in a lot of complex parsing code and provides pretty much nothing to help with this, and if anything actually puts roadblocks in the way.

replies(4): >>14861447 #>>14863535 #>>14864749 #>>14872582 #
14. corndoge ◴[] No.14861447{3}[source]
I shot myself in the foot yet I don't blame the manufacturer for not putting a safety on the gun since I'm the one that bought it with full knowledge of the caveats
replies(4): >>14861489 #>>14861731 #>>14861881 #>>14861917 #
15. teraflop ◴[] No.14861489{4}[source]
If thousands of people were repeatedly making the same mistake, and frequently shooting other innocent bystanders' feet, I would definitely put some blame on the manufacturer.
replies(1): >>14862397 #
16. armitron ◴[] No.14861607{4}[source]
I hate the term "high level assembly" when applied to C since it's loaded against assembly (in the sense of C as its superset) which is obviously not true.

C is full of undefined behavior, assembly is not.

replies(2): >>14863893 #>>14864773 #
17. simonh ◴[] No.14861731{4}[source]
That doesn't mean that gun is well designed or worth buying and using. Criticising the lack of such a valuable or even essential feature and advising people not to use it would be fair comment. Right?
replies(1): >>14862394 #
18. sbierwagen ◴[] No.14861881{4}[source]
You consented to it, sure. This bug affected millions of phones. It's more like a car manufacturer decided to put a particular explode-y gas tank in a car, because it provided greater performance.
replies(1): >>14862378 #
19. MaulingMonkey ◴[] No.14861917{4}[source]
My experience is that most C programmers don't know about many of the caveats about the C programming language.

Are you aware that atoi("a"); is undefined behavior? It can crash, it can launch nethack, it can return 0.

replies(1): >>14862385 #
20. MaulingMonkey ◴[] No.14861944[source]
> Programmer's mistake for not validating data, not the fault of C language mechanics.

If you outfitted a police force with guns without safeties and hair-pull triggers, and you have any sanity, you're not going to be surprised if the rate of accidental discharges goes up. Programmers use their programming languages a lot more frequently and with a lot less care.

So, sure, let's tell the programmers to be more careful. But the most careful of programmers know they'll still make mistakes, and seek out ways to aid themselves in catching those mistakes instead of hoping they can will them away. One such aid can be choosing another programming language that doesn't share C's language mechanics.

Put another way - programmer's mistake for choosing to use C's language mechanics. You could argue that's technically not saying it's the fault of C language mechanics, but I'd say that's splitting hairs at the best.

You could share the blame with a failure to properly fuzz, and insufficient use of static analysis. I'd be okay with that too.

21. justinjlynn ◴[] No.14861961[source]
There's a reason "THIS SIDE TOWARDS ENEMY" is a thing.
replies(1): >>14868615 #
22. shitgoose ◴[] No.14862014{4}[source]
shouldn't it be:

* (a + b * sizeof(T))

where T is array elements' type?

replies(1): >>14862368 #
23. astrange ◴[] No.14862368{5}[source]
That's implied in + on a pointer. uint32_t* + 1 actually adds 4.
24. astrange ◴[] No.14862377{4}[source]
C doesn't have "memory" in the standard. They're pointers into a contiguous object, but anything before a[-1] or after a[sizeof(a)-1] is undefined aka it actually doesn't exist.
replies(1): >>14885621 #
25. corndoge ◴[] No.14862378{5}[source]
and they put a note in the manual that says "if you drive over 70mph on a hot day it will definitely explode so don't do that"

so if you buy that car and you do that, it's your fault regardless of how poor the car design is

replies(1): >>14862571 #
26. corndoge ◴[] No.14862385{5}[source]
yes I'm aware that parsing a letter as an an integer is undefined behavior, it's in the manual
replies(1): >>14862540 #
27. corndoge ◴[] No.14862394{5}[source]
never said C was a great language, merely indicated that it's important to distinguish between a mistake made by a programmer and a failure of the language

pretty sure trusting user provided data without validation is the programmers fault regardless of language

28. corndoge ◴[] No.14862397{5}[source]
the parents analogy is quite bad since C doesn't shoot you in the foot unless you pull the trigger

closer to a gun without a safety, which plenty of manufacturers sell

29. MaulingMonkey ◴[] No.14862540{6}[source]
> yes I'm aware that parsing a letter as an an integer is undefined behavior

Excellent!

> it's in the manual

It's not in MSDN: https://msdn.microsoft.com/en-us/library/yd5xkb5c.aspx

It's not in the manpages: https://linux.die.net/man/3/atoi

Cppreference understates it has having an undefined return value, rather than undefined behavior outright: http://en.cppreference.com/w/cpp/string/byte/atoi

Tutorialspoint defines the behavior as returning 0, and fresh2refresh makes no mention of undefined behavior.

My eighth google hit for atoi finally, finally, gets it right: http://pubs.opengroup.org/onlinepubs/9699919799/functions/at...

If you buy or pirate a copy of e.g. the C89 standard, or refer to one of the free draft versions, it's of course properly documented there too. Neither shows up in the first 50 google results, naturally.

And, of course, by google result 9, we're back to square one - incorrectly defining the behavior as being "returning 0": https://en.wikibooks.org/wiki/C_Programming/stdlib.h/atoi

30. hedora ◴[] No.14862551{6}[source]
The problem is that a frightening number of people don't bother to write the half-dozen obvious wrappers around this, and stdlib doesn't provide them either:

struct buf { uint8_t * base, size_t size };

replies(2): >>14862626 #>>14862983 #
31. MaulingMonkey ◴[] No.14862571{6}[source]
There's likely to be a government mandated recall if your car "spontaneously" explodes - some designs are so poor they fail to meet entirely reasonably regulatory standards. Burying the lede in the fine print is not a get out of jail free card for obvious reasons.
32. kbenson ◴[] No.14862626{7}[source]
> The problem is that a frightening number of people don't bother to write the half-dozen obvious wrappers around this

That's likely because having to pass it in and out of functions and libs that don't expect your special structure might cause it to have an invalid length, and then all your special wrappers can become a liability and not an advantage through either assuming your bufs are valid, or defensively checking more than is necessarily because they can't know whether it was altered or not.

> stdlib doesn't provide them either

Which is the real problem. That would make them a de facto standard, and a lot (but probably not all) of the problems would be mitigated by people accepting the performance trade offs needed to make them safe.

33. kbart ◴[] No.14862983{7}[source]
That doesn't solve the problem because you still have to set 'size' manually.
34. _pmf_ ◴[] No.14863535{3}[source]
> That analogy sounded better in my head than written down.

Probably, because it's really bad.

35. pmontra ◴[] No.14863893{5}[source]
High level != Superset

Actually we often give up features and specialize with high level languages. That's why there are things easier to do in Ruby than in C++ and vice versa.

They are all (usually much more convenient) subsets of assembly.

36. moocowtruck ◴[] No.14864749{3}[source]
Tools exist that find this error in C code, more to blame the company that didn't use those tools and released code this important. Blaming the language solely... not sure it's productive. If the gun went off in my holster WITH the safety on then I'd blame the manufacturer. Same goes with using the right tools with C, if all steps were taken to do the right thing then I can say we should blame C. But there's not any information here on how broadcom developers their code internally.
replies(1): >>14865601 #
37. antoinealb ◴[] No.14864773{5}[source]
What ? Assembly has undefined behavior, for example wrt unaligned access on some processors.
38. philipodonnell ◴[] No.14865601{4}[source]
Its really hard to find an objective line in these things. Your 'tools exist to find this so they should buy them' is someone else's 'good developers should know this so hire good developers' or 'customers should know to be careful so its their fault' or 'Other languages are better about array length checking so they should use them'.
39. red75prime ◴[] No.14866096[source]
You prefer to blame something that can't realistically be changed instead of something that could be changed. How is it useful?
40. Animats ◴[] No.14868615{3}[source]
It's actually

       FRONT

    TOWARD ENEMY
[1] https://en.wikipedia.org/wiki/M18_Claymore_mine
replies(1): >>14879304 #
41. loonattic ◴[] No.14871609[source]
It's 2017. It's about time we had a better, well designed language (ie Rust, but I'm not sure if it's well designed enough, depends on what you want it for, too). There have been many advances in language research since the 70's that could be very useful for the kind of work C is used for. Saying we should stick to C because it's well established is like saying we should stick to assembly if it was well established. Except it's not portable, but close enough.
replies(1): >>14877927 #
42. vsl ◴[] No.14872582{3}[source]
> that keeps going off

If the C language (not even a compiler, the language) somehow magically inserted bad code or rewrote your good code to be buggy on its own, you'd have a point.

43. throwaway47861 ◴[] No.14877927{3}[source]
I've noticed that C and C++ have pretty fanatical and very narrow-minded fanbase. And I am talking about people of ages 50+ as well -- I was acquainted with several of them (in the real physical world) as well.

So IMO it's absolutely pointless trying to argue with them in the first place. They are set in their ways and while a good chunk of them are pretty strict and excellent in what they do, they are not open to any changes.

I would be the first to agree that Go and Rust aren't ready to start replacing drivers but IMO people should start trying! (Or invest in LLVM some more?)

C/C++'s faults aren't ever going away. They're too convenient in their target area. I am against the overly-used "disruption" term -- I happen to believe the USA tech blogosphere bastardized the term long ago -- but IMO the systems programming area is very, VERY overdue for disruption.

It's time.

44. justinjlynn ◴[] No.14879304{4}[source]
Fair enough. Thanks for the correction and additional information.
45. DiThi ◴[] No.14885621{5}[source]
`sizeof(a)` only gives the size of the array when the size is specified at compile time. Either you accept e.g. `int[16]` as a type, or you pass a pointer (for which `sizeof` just returns `sizeof(intptr_t)`)
replies(1): >>14887915 #
46. nemetroid ◴[] No.14887915{6}[source]
That's not quite right. Arrays always have a knowable length, and sizeof will give a correct result for variable length arrays as well.

However, arrays that are passed as arguments to functions decay into raw pointers, at which point you lose information about its length.

replies(1): >>14890975 #
47. DiThi ◴[] No.14890975{7}[source]
> sizeof will give a correct result for variable length arrays as well.

In C99 with rather spotty support. And never with malloc and similar, which is how the vast majority of arrays are (and can be) created. And you can't return or store those dynamic arrays somewhere else without losing the size info, nor it can be declared static.

In other words, you're right but for very limited situations.