News for hackers. Stuff that matters.
Re:Checked C: extension to C that adds static and dynamic ch... by rootbear on Wednesday March 14, 2018 @09:05PM (#16589739)
Not every string is allocated from the heap. There is no way to know if a string passed to a function, for example, came from the heap or not.
Re:Checked C: extension to C that adds static and dynamic ch... by admax88q on Wednesday March 14, 2018 @09:19PM (#16589813)
That's true, although there's no reason the stack allocator couldn't record this information somewhere as well.

And if the memory allocator doesn't know about the string then you could just revert to the existing behaviour so you're no worse off.

Re:Checked C: extension to C that adds static and dynamic ch... by adrianN on Thursday March 15, 2018 @04:08AM (#16591126)
Clang offers a memory sanitizer that does a lot of extra checking. People don't ship their software with the sanitizer activated because the performance hit is nontrivial.
Re:Checked C: extension to C that adds static and dynamic ch... by xtrapolate on Thursday March 15, 2018 @06:38AM (#16591653)
You're free to do as you please here. You don't have to use malloc()/free(), just implement your own LIBRARY_Alloc()/LIBRARY_Free()/LIBRARY_GetMemorySizeBytes() :)
Re:Checked C: extension to C that adds static and dynamic ch... by chrisseaton on Wednesday March 14, 2018 @09:37PM (#16589911)
There's more ways to allocate memory than the heap and stack.
Re:Checked C: extension to C that adds static and dynamic ch... by stochastic_monk on Wednesday March 14, 2018 @09:52PM (#16589970)
What are you referring to? sbrk? mmap? I typically use either the heap or the stack myself.
Re:Checked C: extension to C that adds static and dynamic ch... by MarceColl on Thursday March 15, 2018 @04:22AM (#16591164)
Something like memory pools or manual memory management. Yes the memory pool will come from the heap or stack but the heap/stack allocator will have no idea what is going on in there.
Re:Checked C: extension to C that adds static and dynamic ch... by timClicks on Wednesday March 14, 2018 @11:27PM (#16590225)
Static memory is at the bottom of the address space, below the heap. But that would really only be relevant in the case of string literals unless I'm mistaken.
Re:Checked C: extension to C that adds static and dynamic ch... by unwind on Thursday March 15, 2018 @04:10AM (#16591129)
You can have a static buffer:

    static char buf[128];
and use strcpy() at any point to copy into it, so I don't think statically allocated strings must be literals.
Re:Checked C: extension to C that adds static and dynamic ch... by stochastic_monk on Thursday March 15, 2018 @09:52AM (#16592812)
There is a distinction between const and non-const static memory, though, in that the former can be stored in Read-Only Memory. Still, static memory is generally used for many fewer purposes than the stack or the heap, and if I understand correctly, sbrk is still working with the heap, just more manually.
Re:Checked C: extension to C that adds static and dynamic ch... by timClicks on Thursday March 15, 2018 @08:17PM (#16597652)
That's right. Allocators use the sbrk and mmap to manage the heap. IIRC high performance allocators tend to use mmap exclusively.
Re:Checked C: extension to C that adds static and dynamic ch... by admax88q on Tuesday March 20, 2018 @12:29AM (#16626075)
Okay but you don't have to implement a 100% solution to make 90% of existing code much safer.

How much C code is out there that only uses malloc/free? All of those programs could be made safer if the default string methods were able to prevent writing past the end of the array.

Re:Checked C: extension to C that adds static and dynamic ch... by adrianmonk on Thursday March 15, 2018 @01:30AM (#16590606)
And all of them know how much memory they've allocated.
Re:Checked C: extension to C that adds static and dynamic ch... by MarceColl on Thursday March 15, 2018 @04:26AM (#16591173)
Yes, but unless you expose at the language level an interface for all types of allocators and enforce it then a whole lot of allocation techniques like memory pools couldn't be used with strcpy or would have to use char* type of strings again.

The point is not that allocators don't know how much memory has been allocated, but that C has no idea about the underlying allocator, so it cannot ask it about the length of the string as it was proposed.

Re:Checked C: extension to C that adds static and dynamic ch... by Gibbon1 on Thursday March 15, 2018 @01:47AM (#16590666)
However that doesn't mean that you shouldn't be able to either

Would be nice to be able to tell if something is allocated on the heap or stack in a portable way.

As in being able to ask if and object is thread local or not. The language and run time could support that but studiously does not.

Just like the language could allow you to directly monkey with the ABI but does not.

Re:Checked C: extension to C that adds static and dynamic ch... by wruza on Thursday March 15, 2018 @04:47AM (#16591231)
That something could be allocated by custom allocator like new or g_slice_alloc. You can monkey with the ABI as numerous FFI libraries do, but it is not portable by definition, unless you’re using, well, portable FFI library. Personally I can’t see why string, abi, etc libraries must be included. Include GLib++/GObject++ then and use its actually large ecosystem — what problem are you trying to solve by blaming low-level? That low-level is too low? Do you need high-level low-level that everyone will obey next morning? Check Java?
Re:Checked C: extension to C that adds static and dynamic ch... by Gibbon1 on Thursday March 15, 2018 @10:14PM (#16598081)
> portable FFI library

I looked into that, not available on many many platforms.