問題は非常に単純です:与えられたstart_index
との組み合わせを使用して、要素count
を持つ配列に安全にアクセスできるかどうかを確認したいと思います。length
とりあえず持っているものは以下です。
uint32_t start_index = (value from somewhere);
uint32_t count = (value from somewhere);
uint32_t length = (value set earlier);
char *array = (memory allocated earlier);
if(start_index + count < length) {
// access array starting at start_index
} else {
// bailout
}
start_index + count
uint32_t の最大可能値を超えて小さな値にラップアラウンドする可能性があるため、チェックはもちろん不十分です。これを修正するには、変数を 64 ビットにプロモートするか、2 つ目の条件を設定する方が効率的かどうか疑問に思いますstart_index + count > start_index
。それとも、これを処理するための他の賢い方法があるのでしょうか?