Skip to content

Commit aeb5df6

Browse files
ferdymercurylinev
authored andcommitted
[gpad] Fix illegal access to vector.at(-1)
Fixes #18477
1 parent 432c8f5 commit aeb5df6

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

graf2d/gpad/src/TPad.cxx

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -949,16 +949,20 @@ Int_t TPad::ClippingCode(Double_t x, Double_t y, Double_t xcl1, Double_t ycl1, D
949949

950950
Int_t TPad::ClipPolygon(Int_t n, Double_t *x, Double_t *y, Int_t nn, Double_t *xc, Double_t *yc, Double_t xclipl, Double_t yclipb, Double_t xclipr, Double_t yclipt)
951951
{
952+
if (n <= 0)
953+
return 0;
954+
952955
Int_t nc, nc2;
953956
Double_t x1, y1, x2, y2, slope; // Segment to be clipped
954957

955958
std::vector<Double_t> xc2(nn), yc2(nn);
956959

957960
// Clip against the left boundary
958-
x1 = x[n-1]; y1 = y[n-1];
961+
x1 = x[n - 1];
962+
y1 = y[n - 1];
959963
nc2 = 0;
960964
Int_t i;
961-
for (i=0; i<n; i++) {
965+
for (i = 0; i < n; i++) {
962966
x2 = x[i]; y2 = y[i];
963967
if (x1 == x2) {
964968
slope = 0;
@@ -981,9 +985,12 @@ Int_t TPad::ClipPolygon(Int_t n, Double_t *x, Double_t *y, Int_t nn, Double_t *x
981985
}
982986

983987
// Clip against the top boundary
984-
x1 = xc2[nc2-1]; y1 = yc2[nc2-1];
988+
if (nc2 > 0) {
989+
x1 = xc2[nc2 - 1];
990+
y1 = yc2[nc2 - 1];
991+
}
985992
nc = 0;
986-
for (i=0; i<nc2; i++) {
993+
for (i = 0; i < nc2; i++) {
987994
x2 = xc2[i]; y2 = yc2[i];
988995
if (y1 == y2) {
989996
slope = 0;
@@ -1005,12 +1012,12 @@ Int_t TPad::ClipPolygon(Int_t n, Double_t *x, Double_t *y, Int_t nn, Double_t *x
10051012
x1 = x2; y1 = y2;
10061013
}
10071014

1008-
if (nc>0) {
1009-
1010-
// Clip against the right boundary
1011-
x1 = xc[nc-1]; y1 = yc[nc-1];
1015+
// Clip against the right boundary
1016+
if (nc > 0) {
1017+
x1 = xc[nc - 1];
1018+
y1 = yc[nc - 1];
10121019
nc2 = 0;
1013-
for (i=0; i<nc; i++) {
1020+
for (i = 0; i < nc; i++) {
10141021
x2 = xc[i]; y2 = yc[i];
10151022
if (x1 == x2) {
10161023
slope = 0;
@@ -1033,9 +1040,12 @@ Int_t TPad::ClipPolygon(Int_t n, Double_t *x, Double_t *y, Int_t nn, Double_t *x
10331040
}
10341041

10351042
// Clip against the bottom boundary
1036-
x1 = xc2[nc2-1]; y1 = yc2[nc2-1];
1043+
if (nc2 > 0) {
1044+
x1 = xc2[nc2 - 1];
1045+
y1 = yc2[nc2 - 1];
1046+
}
10371047
nc = 0;
1038-
for (i=0; i<nc2; i++) {
1048+
for (i = 0; i < nc2; i++) {
10391049
x2 = xc2[i]; y2 = yc2[i];
10401050
if (y1 == y2) {
10411051
slope = 0;
@@ -1058,7 +1068,8 @@ Int_t TPad::ClipPolygon(Int_t n, Double_t *x, Double_t *y, Int_t nn, Double_t *x
10581068
}
10591069
}
10601070

1061-
if (nc < 3) nc =0;
1071+
if (nc < 3)
1072+
nc = 0;
10621073
return nc;
10631074
}
10641075

0 commit comments

Comments
 (0)