memononen / nanosvg

Simple stupid SVG parser
zlib License
1.71k stars 363 forks source link

NSVGgradient stops array always has only one item in it? #170

Open aaronfranke opened 5 years ago

aaronfranke commented 5 years ago

It seems like a bug to have an array with exactly one item, what's the point?

typedef struct NSVGgradient {
    float xform[6];
    char spread;
    float fx, fy;
    int nstops;
    NSVGgradientStop stops[1]; // here
} NSVGgradient;
tesch1 commented 5 years ago

Have you looked at the code to see why it might be done like that?

aaronfranke commented 5 years ago

It was introduced 6 years ago here, cc @memononen

tesch1 commented 5 years ago

Do you even C, bro?

Here's a hint:

grad = (NSVGgradient*)malloc(sizeof(NSVGgradient) + sizeof(NSVGgradientStop)*(nstops-1));

Btw, this project isn't actively maintained.

memononen commented 5 years ago

It's "oldskool" trick to create variable length array without extra pointer and alloc.

On Sat, Nov 16, 2019 at 4:41 AM Aaron Franke notifications@github.com wrote:

It seems like a bug to have an array with exactly one item, what's the point?

typedef struct NSVGgradient { float xform[6]; char spread; float fx, fy; int nstops; NSVGgradientStop stops[1]; // here } NSVGgradient;

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/memononen/nanosvg/issues/170?email_source=notifications&email_token=ABIBXSD4UMHYLX7N7PJDEVTQT5MVDA5CNFSM4JODB6VKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HZYG44Q, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABIBXSGQ7E67IXISRBK6Z4DQT5MVDANCNFSM4JODB6VA .

derekdai commented 3 years ago

after change from stops[1] to stops[0] and nstops-1 to nstops (gcc), it explicit tell address sanitizer it is a variable size array, no warning message generated.

typedef struct NSVGgradient {
    float xform[6];
    char spread;
    float fx, fy;
    int nstops;
    NSVGgradientStop stops[0]; // here
} NSVGgradient;
grad = (NSVGgradient*)malloc(sizeof(NSVGgradient) + sizeof(NSVGgradientStop)*(nstops));