nanosvg icon indicating copy to clipboard operation
nanosvg copied to clipboard

NSVGgradient stops array always has only one item in it?

Open aaronfranke opened this issue 4 years ago • 5 comments

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;

aaronfranke avatar Nov 16 '19 02:11 aaronfranke

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

tesch1 avatar Nov 16 '19 15:11 tesch1

It was introduced 6 years ago here, cc @memononen

aaronfranke avatar Nov 16 '19 18:11 aaronfranke

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.

tesch1 avatar Nov 16 '19 19:11 tesch1

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 [email protected] 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 .

memononen avatar Nov 18 '19 18:11 memononen

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

derekdai avatar Mar 14 '21 08:03 derekdai