# Ngx\_array zero capacity support

**URL:** <https://www.ruby-forum.com/t/ngx-array-zero-capacity-support/175349>\
**Category:** NGINX\
**Created:** [September 11, 2009, 4:53pm UTC](https://www.ruby-forum.com/t/ngx-array-zero-capacity-support/175349 "2009-09-11T16:53:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_SchSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/df788c/32.png) [@Peter\_SchSSS\_ller](https://www.ruby-forum.com/u/Peter_SchSSS_ller)\
**Post date:** [September 11, 2009, 4:53pm UTC](https://www.ruby-forum.com/t/ngx-array-zero-capacity-support/175349/1 "2009-09-11T16:53:02Z")

</div>

Hello,

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).

---

<div class="post-metadata">

**Author:** ![Peter\_SchSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/df788c/32.png) [@Peter\_SchSSS\_ller](https://www.ruby-forum.com/u/Peter_SchSSS_ller)\
**Post date:** [September 11, 2009, 7:13pm UTC](https://www.ruby-forum.com/t/ngx-array-zero-capacity-support/175349/2 "2009-09-11T19:13:33Z")

</div>

Hello!

On Fri, Sep 11, 2009 at 04:43:00PM +0200, Peter SchÃ¼ller wrote:

> Hello,
> 
> 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?

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

Maxim D.

---

<div class="post-metadata">

**Author:** ![Peter\_SchSSS\_ller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/df788c/32.png) [@Peter\_SchSSS\_ller](https://www.ruby-forum.com/u/Peter_SchSSS_ller)\
**Post date:** [September 11, 2009, 11:36pm UTC](https://www.ruby-forum.com/t/ngx-array-zero-capacity-support/175349/3 "2009-09-11T23:36:23Z")

</div>

Hello,

> > 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.
