次の不変条件を仮定できますか?
void foo(char *buf, size_t len) {
// "buf" points to either an array or memory allocated with malloc().
assert((uintptr_t)(buf + len) < UINTPTR_MAX);
}
私が書いているパーサーでは、ポインターを使用して特定のオフセットをマークしたいと考えています。たとえばchar *end_of_submessage
、end_of_submessage
は現在のバッファーに相対的です。ただし、サブメッセージが現在のバッファー内で終了しない場合は、現在のバッファー内のオフセットよりも大きい値を使用したいと考えています。だから私はするだろう:
void parse(char *buf, size_t len, uintptr_t end_of_submessage) {
// Some parsing that might increment "buf"
// ...
// If end_of_submessage == UINTPTR_MAX, processing will not be
// triggered even if we have processed our entire current buffer.
if ((uintptr_t)buf >= end_of_submessage)
process_submsg_end();
}
ptr + len == UINTPTR_MAX
しかし、malloc() が のようなメモリを返した場合、または配列が同じプロパティを持っていた場合、このスキームは阻止されます。これが決して起こらないと仮定しても安全ですか?規格上安全ですか?そうでない場合、実際には安全ですか?