Mercurial > octave
comparison libinterp/corefcn/gl2ps-print.cc @ 30262:f43902a87bf1
gl2ps-print.cc: Avoid integer overflow in multiplication.
* libinterp/corefcn/gl2ps-print.cc (gl2ps_renderer::draw_image,
gl2ps_renderer::draw_pixels): Cast integer size argument of
OCTAVE_LOCAL_BUFFER to size_t before multiplication to avoid integer overflow.
The overflow is very unlikely to happen. But it's an easy fix that doesn't
clutter the code too much.
author | Markus Mützel <markus.muetzel@gmx.de> |
---|---|
date | Sun, 31 Oct 2021 12:13:23 +0100 |
parents | 4b25d83d152d |
children | 91c6288781ba a61e1a0f6024 |
comparison
equal
deleted
inserted
replaced
30261:a49c635b179d | 30262:f43902a87bf1 |
---|---|
1364 { | 1364 { |
1365 if (cdata.is_double_type ()) | 1365 if (cdata.is_double_type ()) |
1366 { | 1366 { |
1367 const NDArray xcdata = cdata.array_value (); | 1367 const NDArray xcdata = cdata.array_value (); |
1368 | 1368 |
1369 OCTAVE_LOCAL_BUFFER (GLfloat, a, 3*(j1-j0)*(i1-i0)); | 1369 OCTAVE_LOCAL_BUFFER (GLfloat, a, |
1370 static_cast<size_t> (3)*(j1-j0)*(i1-i0)); | |
1370 | 1371 |
1371 for (int i = i0; i < i1; i++) | 1372 for (int i = i0; i < i1; i++) |
1372 { | 1373 { |
1373 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3) | 1374 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3) |
1374 { | 1375 { |
1393 } | 1394 } |
1394 else if (cdata.is_single_type ()) | 1395 else if (cdata.is_single_type ()) |
1395 { | 1396 { |
1396 const FloatNDArray xcdata = cdata.float_array_value (); | 1397 const FloatNDArray xcdata = cdata.float_array_value (); |
1397 | 1398 |
1398 OCTAVE_LOCAL_BUFFER (GLfloat, a, 3*(j1-j0)*(i1-i0)); | 1399 OCTAVE_LOCAL_BUFFER (GLfloat, a, |
1400 static_cast<size_t> (3)*(j1-j0)*(i1-i0)); | |
1399 | 1401 |
1400 for (int i = i0; i < i1; i++) | 1402 for (int i = i0; i < i1; i++) |
1401 { | 1403 { |
1402 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3) | 1404 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3) |
1403 { | 1405 { |
1422 } | 1424 } |
1423 else if (cdata.is_uint8_type ()) | 1425 else if (cdata.is_uint8_type ()) |
1424 { | 1426 { |
1425 const uint8NDArray xcdata = cdata.uint8_array_value (); | 1427 const uint8NDArray xcdata = cdata.uint8_array_value (); |
1426 | 1428 |
1427 OCTAVE_LOCAL_BUFFER (GLubyte, a, 3*(j1-j0)*(i1-i0)); | 1429 OCTAVE_LOCAL_BUFFER (GLubyte, a, |
1430 static_cast<size_t> (3)*(j1-j0)*(i1-i0)); | |
1428 | 1431 |
1429 for (int i = i0; i < i1; i++) | 1432 for (int i = i0; i < i1; i++) |
1430 { | 1433 { |
1431 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3) | 1434 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3) |
1432 { | 1435 { |
1451 } | 1454 } |
1452 else if (cdata.is_uint16_type ()) | 1455 else if (cdata.is_uint16_type ()) |
1453 { | 1456 { |
1454 const uint16NDArray xcdata = cdata.uint16_array_value (); | 1457 const uint16NDArray xcdata = cdata.uint16_array_value (); |
1455 | 1458 |
1456 OCTAVE_LOCAL_BUFFER (GLushort, a, 3*(j1-j0)*(i1-i0)); | 1459 OCTAVE_LOCAL_BUFFER (GLushort, a, |
1460 static_cast<size_t> (3)*(j1-j0)*(i1-i0)); | |
1457 | 1461 |
1458 for (int i = i0; i < i1; i++) | 1462 for (int i = i0; i < i1; i++) |
1459 { | 1463 { |
1460 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3) | 1464 for (int j = j0, idx = (i-i0)*(j1-j0)*3; j < j1; j++, idx += 3) |
1461 { | 1465 { |
1488 | 1492 |
1489 void | 1493 void |
1490 gl2ps_renderer::draw_pixels (int w, int h, const float *data) | 1494 gl2ps_renderer::draw_pixels (int w, int h, const float *data) |
1491 { | 1495 { |
1492 // Clip data between 0 and 1 for float values | 1496 // Clip data between 0 and 1 for float values |
1493 OCTAVE_LOCAL_BUFFER (float, tmp_data, 3*w*h); | 1497 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h); |
1494 | 1498 |
1495 for (int i = 0; i < 3*h*w; i++) | 1499 for (int i = 0; i < 3*h*w; i++) |
1496 tmp_data[i] = (data[i] < 0.0f ? 0.0f : (data[i] > 1.0f ? 1.0f : data[i])); | 1500 tmp_data[i] = (data[i] < 0.0f ? 0.0f : (data[i] > 1.0f ? 1.0f : data[i])); |
1497 | 1501 |
1498 gl2psDrawPixels (w, h, 0, 0, GL_RGB, GL_FLOAT, tmp_data); | 1502 gl2psDrawPixels (w, h, 0, 0, GL_RGB, GL_FLOAT, tmp_data); |
1501 void | 1505 void |
1502 gl2ps_renderer::draw_pixels (int w, int h, const uint8_t *data) | 1506 gl2ps_renderer::draw_pixels (int w, int h, const uint8_t *data) |
1503 { | 1507 { |
1504 // gl2psDrawPixels only supports the GL_FLOAT type. | 1508 // gl2psDrawPixels only supports the GL_FLOAT type. |
1505 | 1509 |
1506 OCTAVE_LOCAL_BUFFER (float, tmp_data, 3*w*h); | 1510 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h); |
1507 | 1511 |
1508 static const float maxval = std::numeric_limits<uint8_t>::max (); | 1512 static const float maxval = std::numeric_limits<uint8_t>::max (); |
1509 | 1513 |
1510 for (int i = 0; i < 3*w*h; i++) | 1514 for (int i = 0; i < 3*w*h; i++) |
1511 tmp_data[i] = data[i] / maxval; | 1515 tmp_data[i] = data[i] / maxval; |
1516 void | 1520 void |
1517 gl2ps_renderer::draw_pixels (int w, int h, const uint16_t *data) | 1521 gl2ps_renderer::draw_pixels (int w, int h, const uint16_t *data) |
1518 { | 1522 { |
1519 // gl2psDrawPixels only supports the GL_FLOAT type. | 1523 // gl2psDrawPixels only supports the GL_FLOAT type. |
1520 | 1524 |
1521 OCTAVE_LOCAL_BUFFER (float, tmp_data, 3*w*h); | 1525 OCTAVE_LOCAL_BUFFER (float, tmp_data, static_cast<size_t> (3)*w*h); |
1522 | 1526 |
1523 static const float maxval = std::numeric_limits<uint16_t>::max (); | 1527 static const float maxval = std::numeric_limits<uint16_t>::max (); |
1524 | 1528 |
1525 for (int i = 0; i < 3*w*h; i++) | 1529 for (int i = 0; i < 3*w*h; i++) |
1526 tmp_data[i] = data[i] / maxval; | 1530 tmp_data[i] = data[i] / maxval; |