[patch] ngx_array zero capacity support

Peter Schüller scode at spotify.com
Sat Sep 12 01:20:21 MSD 2009


>> attached is a patch to make the ngx_array_push() support zero-capacity
>> arrays. Previously it would fail to grow the data buffer correctly (2
>> * 0 = 0).
> Could you please provide a use case where one should create array
> without elements allocated?

Not necessarily (I can dream up scenarios, but that doesn't matter);
my phrasing in the original post was perhaps a bit misleading.

We had a case of this by accident and while debugging the resulting
problems realized that the array push gave us an invalid pointer. We
do not "need" this particular use case to be supported. However even
so, silently accepting the request and causing memory corruption is
IMO a bug. The request should either be serviced correctly, or an
error should be indicated.

> Current nginx coding style suggest that arrays aren't created
> until needed, and initial allocation chunk must be at least 1.

Sure, which is what we do now and intended to do

An alternative fix is to just translate requests for arrays of
capacity 0 to be of capacity 1. And yet another is to fail the call.

/ Peter Schuller aka scode

More information about the nginx mailing list