I wrote about a simple addition to C that could eliminate most buffer overflows: https://www.digitalmars.com/articles/C-biggest-mistake.html I.e. offering a way that arrays won't automatically decay to pointers when passed as a function parameter.
Arrays are pointers. If they aren't pointers then you need to copy the data when you are giving an array as a function parameter. that's a lot slower. Being able to prepare an set of data in an array and then giving a pointer to a function is very useful. You could add a second type of array on top of what you have in C that includes more stuff, but if that's what you want you can implement that yourself with a struc…
Tell HN: C Experts Panel – Ask us anything about C
621–630 of 978 posts
Re: Tell HN: C Experts Panel – Ask us anything about C
#622In C89 is there a portable way to figure out the alignment requirement for a struct, to be able to, say, store it after the NUL terminator in the same allocation as a C string?
I'm not sure what your requirement is. Usually things work out if you're careful not to assume any specific value for alignment etc. It may mean a few unused bytes here and there, but keeping things simple and portable often pays off.
Re: Tell HN: C Experts Panel – Ask us anything about C
#623When you're looking at an unfamiliar C code base for the first time, how do you approach it? Which files do you look for? Which tools to you open up immediately?
cscope can help
Re: Tell HN: C Experts Panel – Ask us anything about C
#624Is memset(malloc(0), 0, 0) undefined behavior?
Re: Tell HN: C Experts Panel – Ask us anything about C
#625Earlier quoted context omitted.
Arrays are pointers. If they aren't pointers then you need to copy the data when you are giving an array as a function parameter. that's a lot slower. Being able to prepare an set of data in an array and then giving a pointer to a function is very useful. You could add a second type of array on top of what you have in C that includes more stuff, but if that's what you want you can implement that yourself with a struc…
An array is not a pointer. These are completely different data types. For example, you can't apply pointer arithmetic to arrays without casting them to pointers.
Re: Tell HN: C Experts Panel – Ask us anything about C
#626For instance, the macOS clang environment does not define this symbol. Is their implementation of wchar_t or lacking some aspect of Unicode support?
Re: Tell HN: C Experts Panel – Ask us anything about C
#627A few proposals: Why not mandate a warning every time the compiler detects and makes use of UB? It would solve SO many issues. If you are looking to improve security of C programs, then letting the user know what the compiler does should be number one. Try to convert as many UB's to Platform specific, as possible would also be a big help. I would love to see native vector types. Its time. Vector types are now more co…
> Why not mandate a warning every time the compiler detects and makes use of UB? It would solve SO many issues. Because that's hardly ever what happens, except when it actually does, and compilers do an increasingly good job of issuing diagnostics in that case. If you actually mandated it, no compiler today would come close to being standards compliant. This comes close to making the language unimplementable. The mos…
Re: Tell HN: C Experts Panel – Ask us anything about C
#628Earlier quoted context omitted.
That example was not 'overflow'. It was 'off by one'? That seems uninteresting, outside as you say the security issue where somebody might take advantage of it.
That example absolutely was overflow. The bug is, "assert(int+100 > int) optimized away". GCC has the behavior that overflowing a signed integer gives you a negative one. But an if tests that TESTS for that is optimized away! The reason is that overflow is undefined behavior, and therefore they are within their rights to do anything that they want. So they actually overflow in the fastest way possible, and optimize c…
Re: Tell HN: C Experts Panel – Ask us anything about C
#629Re: Tell HN: C Experts Panel – Ask us anything about C
#630Earlier quoted context omitted.
Arrays are pointers. If they aren't pointers then you need to copy the data when you are giving an array as a function parameter. that's a lot slower. Being able to prepare an set of data in an array and then giving a pointer to a function is very useful. You could add a second type of array on top of what you have in C that includes more stuff, but if that's what you want you can implement that yourself with a struc…
An array is not a pointer. These are completely different data types. For example, you can't apply pointer arithmetic to arrays without casting them to pointers.
*(aFoo+3) should work fine and return the 4th int in the array.