stas: (Default)
stas ([personal profile] stas) wrote2016-01-26 04:26 pm

*headdesk*

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?

[identity profile] anatoly borodin (from livejournal.com) 2016-01-27 03:00 am (UTC)(link)
*calloc
ak_47: (default)

[personal profile] ak_47 2016-01-27 04:22 am (UTC)(link)
Это что-то из мира embedded? Потому что аллокация размеров даже близких к величинам где начинается overflow of X*Y выглядит странно, по меньшей мере.

[identity profile] kika.livejournal.com 2016-01-27 04:45 am (UTC)(link)
Странные люди, я всегда calloc() использовал именно как положоно по уставу.

[identity profile] henic.livejournal.com 2016-01-27 07:34 am (UTC)(link)
Какие злые люди. :)

[identity profile] sorhed.livejournal.com 2016-01-27 09:13 am (UTC)(link)
Язык программирования С должен быть разрушен.

[identity profile] laoxia.livejournal.com 2016-01-27 09:21 am (UTC)(link)
в твоем каменте чувствуется обида и зависть, молодой джавист

[identity profile] avnik.livejournal.com 2016-01-27 02:01 pm (UTC)(link)
и джаву тоже разрущить, оставить только лисп и haskell

(no subject)

[identity profile] sorhed.livejournal.com - 2016-01-27 18:24 (UTC) - Expand

(no subject)

[identity profile] sorhed.livejournal.com - 2016-01-27 23:02 (UTC) - Expand

(no subject)

[identity profile] cjelli.livejournal.com - 2016-01-27 21:27 (UTC) - Expand

(no subject)

[identity profile] sorhed.livejournal.com - 2016-01-27 23:00 (UTC) - Expand

[identity profile] trurle.livejournal.com 2016-01-27 11:14 am (UTC)(link)
Нет, это люди неспособные программировать, должны быть направлены на соответствующие их скромным способностям рабочие места. Впрочем, оно так и происходит.

[identity profile] sorhed.livejournal.com 2016-01-27 06:25 pm (UTC)(link)
Кто без сегфолта, пусть первый бросит в меня камень.

[identity profile] sorhed.livejournal.com 2016-01-27 06:24 pm (UTC)(link)
C можно оставить: 1) для эмбеда (раньше всё железо было как эмбед, но сейчас-то...) 2) для ковыряния ядра линукса, потому что оно с нами надолго (несмотря на Erlang on Xen и тому подобные замечательные штуки), 3) для исправления секьюрити-багов, которые наоставляли поколения предыдущих C-кодеров.

Всё, что работает на современных компьютерах и серверах, должно писаться не на C. На выбор: Java/Scala/Erlang/Go/Rust/OCaml/любой другой язык с managed memory.

(no subject)

[identity profile] cjelli.livejournal.com - 2016-01-27 21:32 (UTC) - Expand

(no subject)

[identity profile] cjelli.livejournal.com - 2016-01-28 17:27 (UTC) - Expand

(no subject)

[identity profile] cjelli.livejournal.com - 2016-01-29 00:17 (UTC) - Expand

[identity profile] valshooter.livejournal.com 2016-01-27 10:30 am (UTC)(link)
And what's not the same here? Either we have enough memory to allocate or we don't.

Pardon my ignorance (have very little C experience).

[identity profile] pilpilon.livejournal.com 2016-01-27 10:39 am (UTC)(link)
you could have enough memory but not enough integers to count it

[identity profile] trurle.livejournal.com 2016-01-27 11:15 am (UTC)(link)
Is there are an environment where sizeof(void*) is less than sizeof(size_t)?

(no subject)

[identity profile] pilpilon.livejournal.com - 2016-01-27 11:30 (UTC) - Expand

(no subject)

[identity profile] trurle.livejournal.com - 2016-01-27 11:38 (UTC) - Expand

(no subject)

[identity profile] pappadeux.livejournal.com - 2016-01-31 04:37 (UTC) - Expand

[identity profile] valshooter.livejournal.com 2016-01-27 12:09 pm (UTC)(link)
so basically when I'm calling malloc(X*Y) I need to have X*Y bytes for data + X*4 for pointers?

(4 depends on architecture, I guess)

[identity profile] irene221b.livejournal.com 2016-01-27 12:21 pm (UTC)(link)
The following code excerpt from OpenSSH 3.3 demonstrates a classic case of integer overflow:

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.

[identity profile] valshooter.livejournal.com 2016-01-27 12:25 pm (UTC)(link)
Thank you.

[identity profile] manta.livejournal.com 2016-01-28 08:08 am (UTC)(link)
If nresp has the value 1073741824 and sizeof(char*) has its typical value of 4
... then we are dealing with a 32-bit system, which cannot allocate 4,294,967,296 bytes of memory (4096 GB) to begin with.

(no subject)

[identity profile] irene221b.livejournal.com - 2016-01-28 10:29 (UTC) - Expand

(no subject)

[identity profile] irene221b.livejournal.com - 2016-01-28 22:25 (UTC) - Expand

[identity profile] manta.livejournal.com 2016-01-27 11:59 am (UTC)(link)
sizeof() should return long. Problem solved.

[identity profile] manta.livejournal.com 2016-01-27 05:59 pm (UTC)(link)
So why a size_t isn't 64-bit on 64-bit systems?

(no subject)

[identity profile] manta.livejournal.com - 2016-01-28 08:04 (UTC) - Expand

(no subject)

[identity profile] manta.livejournal.com - 2016-01-28 17:49 (UTC) - Expand

(no subject)

[identity profile] manta.livejournal.com - 2016-01-28 18:55 (UTC) - Expand

(no subject)

[identity profile] cjelli.livejournal.com - 2016-01-29 21:24 (UTC) - Expand

[identity profile] e2pii1.livejournal.com 2016-02-08 03:57 pm (UTC)(link)
с С++ и STL containers уж и забыл когда писал malloc() или new

[identity profile] e2pii1.livejournal.com 2016-02-09 02:21 am (UTC)(link)
EDIT: съело угловые скобки, попробуем так:
EDIT: не помогло - после "cont" X в угловых скобках:

вместо malloc(sizeof(X) * Y) с описанным у вас переполнением в STL будет:
cont contx(Y); с проверкой переполнения и exception,
a вместо malloc(X+Y) будет скорее всего contx.resize(contx.size() + X); contx.resize(contx.size() + Y); , а можно и вообще расширять контейнер по одному элементу по мере их поступления - это достаточно эффективно, ведь malloc будет вызываться не каждый раз, а большими блоками ~log(N) раз.

Edited 2016-02-09 02:25 (UTC)

(no subject)

[identity profile] e2pii1.livejournal.com - 2016-02-16 08:39 (UTC) - Expand

(no subject)

[identity profile] e2pii1.livejournal.com - 2016-02-17 03:06 (UTC) - Expand

(no subject)

[identity profile] e2pii1.livejournal.com - 2016-02-17 09:02 (UTC) - Expand