Bartc said:
Let's say the return from such a function is NULL for a value not part
of any malloc memory, such as addresses of static and frame data.
(Nitpick: you don't mean NULL, since this function isn't supposed to
return a pointer. Let's call it some special value, SIZE_ERR.)
That might require a lot of work to implement, or be impossible,
depending on the implementation.
What if the pointer points within a malloc'ed block, but not to the
beginning? That's harder to recognize.
The gets() implementation which gets a non-NULL value from alloc_size
can then test against the limit (I had in mind the address of the end
of the allocated block), and prevent buffer overflows.
If it gets a NULL value, then it can work as before. So gets() would
be safe only when used with allocated memory.
This means your alloc_size must never have a false positive; it must
always return either the correct size or SIZE_ERR. This probably means
that somewhere there needs to be a list of all malloc'ed blocks. Many
implementations don't maintain such a thing; requiring it would be
expensive in terms of memory, and expensive to search when alloc_size is
called.
Let's imagine a malloc implementation that stores the size of the block
as a size_t immediately preceding the pointer returned by malloc. It
sounds like you're imagining an alloc_size function that might work as
follows:
size_t alloc_size(void *p) {
if (p < heap_start || p >= heap_end)
return SIZE_ERR;
return ((size_t *)p)[-1];
}
Now what happens if the pointer points within a block, and the data just
before it coincidentally looks like a size_t?
char *p = malloc(1000);
size_t s = 3;
memcpy(p, &s, sizeof(s));
gets(p + sizeof(s));
If the line is more than 3 characters, gets() will truncate the line
unnecessarily, or fail in some other way. Truncation would be
especially dangerous because it will result in the program thinking
it's reading data that is different from what's in the file.
All minor details... Ideally alloc_size or alloc_end would only be
used in libraries that know they are available and give useful
results.
Are you proposing that this feature be standard or not? If not, there's
nothing to prevent implementations from providing it of their own
accord, where feasible. But some applications won't find it feasible,
or won't have the guaranteed semantics that you would need to use it in
gets().