Avoid allocating a VLA of size zero

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Avoid allocating a VLA of size zero

Pascal Cuoq
Hello,

Arrays of size zero are not part of standard C, even when they are Variable Length Arrays (https://port70.net/~nsz/c/c11/n1570.html#6.7.6.2p5 ). GCC accepts arrays of size 0 as an extension, but since TCC's small size and being implemented in C in the first place may lead it to be compiled with any C compiler, you may consider it desirable to make sure it's written in portable C when reasonable. This is orthogonal to having TCC accepts GCC extensions, perhaps including this one. What I'm saying is, it can be considered better to have TCC being written in portable C for when it's not compiled with GCC or TCC.


In non-TCC_TARGET_PE mode, the function gfunc_call in the file x86_64-gen.c creates a VLA of size 0 when it is called with nb_args == 0:

void gfunc_call(int nb_args)
{
    ...
    char _onstack[nb_args], *onstack = _onstack;

Assuming you agree it would be preferable not to, this can be fixed in several ways. The change below, for which a patch is attached, may be the one with the least impact:

void gfunc_call(int nb_args)
{
    ....
    int onstack_size = nb_args == 0 ? 1 : nb_args;
    char _onstack[onstack_size], *onstack = _onstack;

Best regards,

Pascal


_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

nb_args_zero.patch (772 bytes) Download Attachment
ATT00001.htm (322 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Avoid allocating a VLA of size zero

Petr Skočík
Nice find, Pascal Cuoq.

I think it looks a bit better without the extra variable:

        char _onstack[nb_args?nb_args:1], *onstack = _onstack;

but then I have been told my overall style sucks.

Cheers,
Petr S.


On 6/12/19 10:39 PM, Pascal Cuoq wrote:

> Hello,
>
> Arrays of size zero are not part of standard C, even when they are
> Variable Length Arrays
> (https://port70.net/~nsz/c/c11/n1570.html#6.7.6.2p5 ). GCC accepts
> arrays of size 0 as an extension, but since TCC's small size and being
> implemented in C in the first place may lead it to be compiled with any
> C compiler, you may consider it desirable to make sure it's written in
> portable C when reasonable. This is orthogonal to having TCC accepts GCC
> extensions, perhaps including this one. What I'm saying is, it can be
> considered better to have TCC being written in portable C for when it's
> not compiled with GCC or TCC.
>
>
> In non-TCC_TARGET_PE mode, the function gfunc_call in the file
> x86_64-gen.c creates a VLA of size 0 when it is called with nb_args == 0:
>
> void gfunc_call(int nb_args)
> {
>     ...
>     char _onstack[nb_args], *onstack = _onstack;
>
> Assuming you agree it would be preferable not to, this can be fixed in
> several ways. The change below, for which a patch is attached, may be
> the one with the least impact:
>
> void gfunc_call(int nb_args)
> {
>     ....
>     int onstack_size = nb_args == 0 ? 1 : nb_args;
>     char _onstack[onstack_size], *onstack = _onstack;
>
> Best regards,
>
> Pascal
>
>
> _______________________________________________
> Tinycc-devel mailing list
> [hidden email]
> https://lists.nongnu.org/mailman/listinfo/tinycc-devel
>


_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
Reply | Threaded
Open this post in threaded view
|

Re: Avoid allocating a VLA of size zero

Michael Matz-4
Hi,

On Wed, 12 Jun 2019, Petr Skočík wrote:

> Nice find, Pascal Cuoq.
>
> I think it looks a bit better without the extra variable:
>
> char _onstack[nb_args?nb_args:1], *onstack = _onstack;

Agreed, if you put spaces around the ? and : ;)


Ciao,
Michael.
_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
Reply | Threaded
Open this post in threaded view
|

Re: Avoid allocating a VLA of size zero

Pascal Cuoq
Hello,

> On 16 Jun 2019, at 21:49, Michael Matz <[hidden email]> wrote:
>
> On Wed, 12 Jun 2019, Petr Skočík wrote:
>
>> I think it looks a bit better without the extra variable:
>>
>> char _onstack[nb_args?nb_args:1], *onstack = _onstack;
>
> Agreed, if you put spaces around the ? and : ;)

So I pushed that patch as my first commit. If the git is broken now it's Petr's fault for pointing out that anyone can push.

Pascal

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel
Reply | Threaded
Open this post in threaded view
|

Re: Avoid allocating a VLA of size zero

Christian Jullien-3
Thank you for your first contribution to tcc ... and mod is not broken :o))

A first step starting a long journey?

C.

-----Original Message-----
From: Tinycc-devel [mailto:tinycc-devel-bounces+eligis=[hidden email]] On Behalf Of Pascal Cuoq
Sent: Wednesday, June 19, 2019 21:07
To: Michael Matz
Cc: [hidden email]
Subject: Re: [Tinycc-devel] Avoid allocating a VLA of size zero

Hello,

> On 16 Jun 2019, at 21:49, Michael Matz <[hidden email]> wrote:
>
> On Wed, 12 Jun 2019, Petr Skočík wrote:
>
>> I think it looks a bit better without the extra variable:
>>
>> char _onstack[nb_args?nb_args:1], *onstack = _onstack;
>
> Agreed, if you put spaces around the ? and : ;)

So I pushed that patch as my first commit. If the git is broken now it's Petr's fault for pointing out that anyone can push.

Pascal

_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel


_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel