Skip to content

Commit 4974a24

Browse files
committed
Fix "out of bounds" undefined behavior
This is detected with clang and -fsanitizer=bounds. Out-of-bound addressing array of fixed size is undefined behavior in both C and C++. It is so even if underlying memory is properly allocated. To avoid undefined behavior in cases like this C99 added feature called "flexible array member" when size of array is omitted. Unfortunately it's not C++ feature. However both clang++ and g++ this and this seems the only reasonable way to avoid undefined behavior without significant rewriting such code.
1 parent 2632ac9 commit 4974a24

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

extern/agg24-svn/include/agg_pixfmt_rgba.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ namespace agg
15211521
};
15221522
struct pixel_type
15231523
{
1524-
value_type c[num_components];
1524+
value_type c[];
15251525

15261526
void set(value_type r, value_type g, value_type b, value_type a)
15271527
{
@@ -1733,12 +1733,10 @@ namespace agg
17331733
unsigned len,
17341734
const color_type& c)
17351735
{
1736-
pixel_type v;
1737-
v.set(c);
17381736
pixel_type* p = pix_value_ptr(x, y, len);
17391737
do
17401738
{
1741-
*p = v;
1739+
p->set(c);
17421740
p = p->next();
17431741
}
17441742
while (--len);
@@ -1750,11 +1748,9 @@ namespace agg
17501748
unsigned len,
17511749
const color_type& c)
17521750
{
1753-
pixel_type v;
1754-
v.set(c);
17551751
do
17561752
{
1757-
*pix_value_ptr(x, y++, 1) = v;
1753+
pix_value_ptr(x, y++, 1)->set(c);
17581754
}
17591755
while (--len);
17601756
}
@@ -1770,11 +1766,9 @@ namespace agg
17701766
pixel_type* p = pix_value_ptr(x, y, len);
17711767
if (c.is_opaque() && cover == cover_mask)
17721768
{
1773-
pixel_type v;
1774-
v.set(c);
17751769
do
17761770
{
1777-
*p = v;
1771+
p->set(c);
17781772
p = p->next();
17791773
}
17801774
while (--len);
@@ -1814,11 +1808,9 @@ namespace agg
18141808
{
18151809
if (c.is_opaque() && cover == cover_mask)
18161810
{
1817-
pixel_type v;
1818-
v.set(c);
18191811
do
18201812
{
1821-
*pix_value_ptr(x, y++, 1) = v;
1813+
pix_value_ptr(x, y++, 1)->set(c);
18221814
}
18231815
while (--len);
18241816
}

0 commit comments

Comments
 (0)