Tuesday, January 26th, 2016 04:26 pm
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)
Wednesday, January 27th, 2016 03:00 am (UTC)
*calloc
Wednesday, January 27th, 2016 04:22 am (UTC)
Это что-то из мира embedded? Потому что аллокация размеров даже близких к величинам где начинается overflow of X*Y выглядит странно, по меньшей мере.
Wednesday, January 27th, 2016 04:45 am (UTC)
Странные люди, я всегда calloc() использовал именно как положоно по уставу.
Wednesday, January 27th, 2016 07:34 am (UTC)
Какие злые люди. :)
Wednesday, January 27th, 2016 09:13 am (UTC)
Язык программирования С должен быть разрушен.
Wednesday, January 27th, 2016 09:21 am (UTC)
в твоем каменте чувствуется обида и зависть, молодой джавист
Wednesday, January 27th, 2016 02:01 pm (UTC)
и джаву тоже разрущить, оставить только лисп и haskell

(no subject)

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

(no subject)

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

(no subject)

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

(no subject)

[identity profile] sorhed.livejournal.com - 2016-01-27 11:00 pm (UTC) - Expand
Wednesday, January 27th, 2016 11:14 am (UTC)
Нет, это люди неспособные программировать, должны быть направлены на соответствующие их скромным способностям рабочие места. Впрочем, оно так и происходит.
Wednesday, January 27th, 2016 06:25 pm (UTC)
Кто без сегфолта, пусть первый бросит в меня камень.
Wednesday, January 27th, 2016 06:24 pm (UTC)
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 09:32 pm (UTC) - Expand

(no subject)

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

(no subject)

[identity profile] cjelli.livejournal.com - 2016-01-29 12:17 am (UTC) - Expand
Wednesday, January 27th, 2016 10:30 am (UTC)
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).
Wednesday, January 27th, 2016 10:39 am (UTC)
you could have enough memory but not enough integers to count it
Wednesday, January 27th, 2016 11:15 am (UTC)
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 am (UTC) - Expand

(no subject)

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

(no subject)

[identity profile] pappadeux.livejournal.com - 2016-01-31 04:37 am (UTC) - Expand
Wednesday, January 27th, 2016 12:09 pm (UTC)
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)
Wednesday, January 27th, 2016 12:21 pm (UTC)
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.
Wednesday, January 27th, 2016 12:25 pm (UTC)
Thank you.
Thursday, January 28th, 2016 08:08 am (UTC)
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 am (UTC) - Expand

(no subject)

[identity profile] irene221b.livejournal.com - 2016-01-28 10:25 pm (UTC) - Expand
Wednesday, January 27th, 2016 11:59 am (UTC)
sizeof() should return long. Problem solved.
Wednesday, January 27th, 2016 05:59 pm (UTC)
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 am (UTC) - Expand

(no subject)

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

(no subject)

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

(no subject)

[identity profile] cjelli.livejournal.com - 2016-01-29 09:24 pm (UTC) - Expand
Monday, February 8th, 2016 03:57 pm (UTC)
с С++ и STL containers уж и забыл когда писал malloc() или new
Tuesday, February 9th, 2016 02:21 am (UTC)
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 am (UTC)

(no subject)

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

(no subject)

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

(no subject)

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