So, you've got a code in C that needs to allocate X objects of size Y. So, you start with malloc(X*Y). Then you discover X*Y may produce overflow, so you make function alloc(X, Y) that checks for overflows.
And then of course everybody starts using it as alloc(1, X*Y). Because it's the same thing, right?
And then of course everybody starts using it as alloc(1, X*Y). Because it's the same thing, right?
no subject
no subject
no subject
no subject
no subject
no subject
no subject
no subject
Pardon my ignorance (have very little C experience).
no subject
no subject
no subject
no subject
no subject
no subject
no subject
(4 depends on architecture, I guess)
no subject
nresp = packet_get_int();
if (nresp > 0) {
response = xmalloc(nresp*sizeof(char*));
for (i = 0; i < nresp; i++)
response[i] = packet_get_string(NULL);
}
If nresp has the value 1073741824 and sizeof(char*) has its typical value of 4, then the result of the operation nresp*sizeof(char*) overflows, and the argument to xmalloc() will be 0. Most malloc() implementations will happily allocate a 0-byte buffer, causing the subsequent loop iterations to overflow the heap buffer response.
no subject
no subject
no subject
no subject
no subject
no subject
no subject
Всё, что работает на современных компьютерах и серверах, должно писаться не на C. На выбор: Java/Scala/Erlang/Go/Rust/OCaml/любой другой язык с managed memory.
no subject
no subject