|
13 | 13 | #include "debug-internal.h" |
14 | 14 |
|
15 | 15 |
|
| 16 | +// |
| 17 | +// Limits... |
| 18 | +// |
| 19 | + |
| 20 | +#define _CUPS_MAX_BYTES_PER_LINE (16 * 1024 * 1024) |
| 21 | +#define _CUPS_MAX_BITS_PER_COLOR 16 |
| 22 | +#define _CUPS_MAX_BITS_PER_PIXEL 240 |
| 23 | + |
| 24 | + |
16 | 25 | // |
17 | 26 | // Private structures... |
18 | 27 | // |
@@ -838,7 +847,7 @@ cupsRasterReadHeader( |
838 | 847 |
|
839 | 848 | memcpy(h, &r->header, sizeof(cups_page_header_t)); |
840 | 849 |
|
841 | | - return (r->header.cupsBitsPerPixel > 0 && r->header.cupsBitsPerPixel <= 240 && r->header.cupsBitsPerColor > 0 && r->header.cupsBitsPerColor <= 16 && r->header.cupsBytesPerLine > 0 && r->header.cupsBytesPerLine <= 0x7fffffff && r->header.cupsHeight != 0 && (r->header.cupsBytesPerLine % r->bpp) == 0); |
| 850 | + return (0); |
842 | 851 | } |
843 | 852 |
|
844 | 853 |
|
@@ -1540,6 +1549,9 @@ cups_raster_read(cups_raster_t *r, // I - Raster stream |
1540 | 1549 | static bool // O - `true` on success, `false` on failure |
1541 | 1550 | cups_raster_update(cups_raster_t *r) // I - Raster stream |
1542 | 1551 | { |
| 1552 | + bool ret = true; // Return value |
| 1553 | + |
| 1554 | + |
1543 | 1555 | if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1 || |
1544 | 1556 | r->header.cupsNumColors == 0) |
1545 | 1557 | { |
@@ -1617,7 +1629,11 @@ cups_raster_update(cups_raster_t *r) // I - Raster stream |
1617 | 1629 |
|
1618 | 1630 | default : |
1619 | 1631 | // Unknown color space |
1620 | | - return (false); |
| 1632 | + _cupsRasterAddError("Unknown color space in page header."); |
| 1633 | + |
| 1634 | + r->header.cupsNumColors = 0; |
| 1635 | + ret = false; |
| 1636 | + break; |
1621 | 1637 | } |
1622 | 1638 |
|
1623 | 1639 | DEBUG_printf("4cups_raster_update: cupsNumColors=%u", r->header.cupsNumColors); |
@@ -1649,28 +1665,72 @@ cups_raster_update(cups_raster_t *r) // I - Raster stream |
1649 | 1665 |
|
1650 | 1666 | DEBUG_printf("4cups_raster_update: remaining=%u", r->remaining); |
1651 | 1667 |
|
| 1668 | + // Validate the page header... |
| 1669 | + if (r->header.cupsBytesPerLine == 0) |
| 1670 | + { |
| 1671 | + _cupsRasterAddError("Invalid raster line length 0."); |
| 1672 | + ret = false; |
| 1673 | + } |
| 1674 | + else if (r->header.cupsBytesPerLine > _CUPS_MAX_BYTES_PER_LINE) |
| 1675 | + { |
| 1676 | + _cupsRasterAddError("Raster line length %u is greater than %d bytes.", r->header.cupsBytesPerLine, _CUPS_MAX_BYTES_PER_LINE); |
| 1677 | + ret = false; |
| 1678 | + } |
| 1679 | + else if ((r->header.cupsBytesPerLine % r->bpp) != 0) |
| 1680 | + { |
| 1681 | + _cupsRasterAddError("Raster line length %u is not a multiple of the pixel size (%d).", r->header.cupsBytesPerLine, r->bpp); |
| 1682 | + ret = false; |
| 1683 | + } |
| 1684 | + |
| 1685 | + if (r->header.cupsBitsPerColor == 0 || r->header.cupsBitsPerColor > _CUPS_MAX_BITS_PER_COLOR) |
| 1686 | + { |
| 1687 | + _cupsRasterAddError("Invalid bits per color %u.", r->header.cupsBitsPerColor); |
| 1688 | + ret = false; |
| 1689 | + } |
| 1690 | + |
| 1691 | + if (r->header.cupsBitsPerPixel == 0 || r->header.cupsBitsPerPixel > _CUPS_MAX_BITS_PER_PIXEL) |
| 1692 | + { |
| 1693 | + _cupsRasterAddError("Invalid bits per pixel %u.", r->header.cupsBitsPerPixel); |
| 1694 | + ret = false; |
| 1695 | + } |
| 1696 | + |
| 1697 | + if (r->header.cupsWidth == 0) |
| 1698 | + { |
| 1699 | + _cupsRasterAddError("Invalid raster width 0."); |
| 1700 | + ret = false; |
| 1701 | + } |
| 1702 | + |
| 1703 | + if (r->header.cupsHeight == 0) |
| 1704 | + { |
| 1705 | + _cupsRasterAddError("Invalid raster height 0."); |
| 1706 | + ret = false; |
| 1707 | + } |
| 1708 | + |
1652 | 1709 | // Allocate the compression buffer... |
1653 | | - if (r->compressed) |
| 1710 | + if (ret && r->compressed) |
1654 | 1711 | { |
1655 | 1712 | free(r->pixels); |
1656 | 1713 |
|
1657 | 1714 | if ((r->pixels = calloc(r->header.cupsBytesPerLine, 1)) == NULL) |
1658 | 1715 | { |
| 1716 | + _cupsRasterAddError("Unable to allocate %u bytes for raster line: %s", r->header.cupsBytesPerLine, strerror(errno)); |
| 1717 | + |
1659 | 1718 | r->pcurrent = NULL; |
1660 | 1719 | r->pend = NULL; |
1661 | 1720 | r->count = 0; |
1662 | | - |
1663 | | - return (false); |
| 1721 | + ret = false; |
1664 | 1722 | } |
| 1723 | + else |
| 1724 | + { |
| 1725 | + r->pcurrent = r->pixels; |
| 1726 | + r->pend = r->pixels + r->header.cupsBytesPerLine; |
| 1727 | + r->count = 0; |
1665 | 1728 |
|
1666 | | - r->pcurrent = r->pixels; |
1667 | | - r->pend = r->pixels + r->header.cupsBytesPerLine; |
1668 | | - r->count = 0; |
1669 | | - |
1670 | | - DEBUG_printf("4cups_raster_update: Allocated %u bytes at %p.", r->header.cupsBytesPerLine, r->pixels); |
| 1729 | + DEBUG_printf("4cups_raster_update: Allocated %u bytes at %p.", r->header.cupsBytesPerLine, r->pixels); |
| 1730 | + } |
1671 | 1731 | } |
1672 | 1732 |
|
1673 | | - return (true); |
| 1733 | + return (ret); |
1674 | 1734 | } |
1675 | 1735 |
|
1676 | 1736 |
|
|
0 commit comments