3 ms·
> "buffer overflow within a struct" is often considered a feature. That's lying to the compiler, because there's no way to say what you meant. That's why I pr
by Animats 3y ago
> "buffer overflow within a struct" is often considered a feature.
That's lying to the compiler, because there's no way to say what you meant.
That's why I proposed the syntax
typedef struct msgitem {
const size_t len;
char itemvalue[len];
};
struct msgitem firstmsg = { 100, 0 }; // empty msgitem, size 100
The last item of a struct could be a variable sized array. That's sound,
and enough for things such as network packets. It's important to avoid bikeshedding.
- dataflow 3y agoWhat do you do when the length isn't a field in the same struct?
- Animats 3y agoThere must be an expression which can be evaluated to determine the length of the array, and which can thus be used for checking. Without that, the code has little chance of working, since something had better define the size of that array.
- dataflow 3y agoI mean the expression doesn't have to come from the same struct, though. It could be provided somewhere else.
- Dylan16807 3y agoIf the somewhere else can't be located in a straightforward way, I'd say just change the code.
- dataflow 3y ago> If the somewhere else can't be located in a straightforward way It might very well be straightforward to obtain, just not located in that struct itself. > I'd say just change the code. And if the code isn't available to you to change?
- Animats 3y agoIf you can't tell how big something is at all, the program is broken and will probably fail randomly.
- dataflow 3y ago> If you can't tell how big something is at all, the program is broken and will probably fail randomly. That's not what I said at all. It's the exact opposite of what I said. Why this strawman? I already replied above and explained very clearly that I'm talking about when the size is known but not via the struct itself: >>> the expression doesn't have to come from the same struct, though. It could be provided somewhere else. >> It might very well be straightforward to obtain, just not located in that struct itself. The size could be communicated in a different struct, no? Or passed back to the caller via a pointer argument? Or a million other ways beside the same struct itself?
- Dylan16807 3y ago> And if the code isn't available to you to change? Then you don't get the improvements in safety yet.
- dataflow 3y ago>> And if the code isn't available to you to change? > Then you don't get the improvements in safety yet. Huh? This isn't a limitation with current implementations like -fbounds-safety. It's just a limitation with the proposal I was pointing out this issue with [1]. The existing implementations decorate the function/usage sites rather than the struct, which gives you access to information outside the struct. And there's no need to change every single use of that struct, which you obviously don't generally have access to. [1] https://news.ycombinator.com/item?id=37799444 https://news.ycombinator.com/item?id=37799444
- Dylan16807 3y agoIt's a limitation off the proposal, sure. I'm saying to deal with it. Change the code to be compatible. It's not that important to keep it the way it is. Now you're referring to better designs, which is great. Have the best of both worlds if that's possible. But when you were just pointing out that difficulty, my response is that it's a very small difficulty so that's not a big mark against the idea. If it was that proposal or nothing, that proposal would be much better than nothing, despite the forced code changes to use it.
- jandrese 3y agoThe "buffer overflow within a struct" is used a lot when you have built a "header" struct that you are just pinning to the start of the some record and the bottom of the struct has a line like: char data[0]; The purpose is to give you a handle on the remainder of the data even though you don't know the size beforehand. Technically this is not valid under strict C, but it's also incredibly common.
- heywhatupboys 3y ago> Technically this is not valid under strict C, but it's also incredibly common. char data[] is valid C in recent versions if it is the last entry of a struct.
- layer8 3y agoTo elaborate, this is called "flexible array member" and was introduced with C99.