From c65e32facc9ade6a2088e227909b7d7a47878a64 Mon Sep 17 00:00:00 2001 From: kargl Date: Thu, 20 Oct 2011 18:07:10 +0000 Subject: [PATCH] 2011-10-20 Steven G. Kargl * gfortran.dg/ishft_3.f90: Update test. 2011-10-20 Steven G. Kargl * check.c (less_than_bitsize1): Check |shift| <= bit_size(i). (gfc_check_ishftc): Check |shift| <= bit_size(i) and check that size is positive. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@180264 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/fortran/ChangeLog | 11 ++++++-- gcc/fortran/check.c | 49 ++++++++++++++++++++++++++++++++++- gcc/testsuite/ChangeLog | 5 ++++ gcc/testsuite/gfortran.dg/ishft_3.f90 | 49 +++++++++++++++++++++++++++-------- 4 files changed, 100 insertions(+), 14 deletions(-) rewrite gcc/testsuite/gfortran.dg/ishft_3.f90 (89%) diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 4f0246275ec..e58dd112aed 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,9 +1,16 @@ -2011-10-30 Steven G. Kargl +2011-10-20 Steven G. Kargl + + PR fortran/50514 + * check.c (less_than_bitsize1): Check |shift| <= bit_size(i). + (gfc_check_ishftc): Check |shift| <= bit_size(i) and check + that size is positive. + +2011-10-20 Steven G. Kargl PR fortran/50524 * resolve.c (resolve_ref): Check return value of resolve_substring(). -2011-10-30 Steven G. Kargl +2011-10-20 Steven G. Kargl * io.c (match_dt_format): Match a user-defined operator or a kind type prefixed string. diff --git a/gcc/fortran/check.c b/gcc/fortran/check.c index bf4559203b1..9aaad01ca36 100644 --- a/gcc/fortran/check.c +++ b/gcc/fortran/check.c @@ -286,6 +286,22 @@ less_than_bitsize1 (const char *arg1, gfc_expr *expr1, const char *arg2, { gfc_extract_int (expr2, &i2); i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false); + + /* For ISHFT[C], check that |shift| <= bit_size(i). */ + if (arg2 == NULL) + { + if (i2 < 0) + i2 = -i2; + + if (i2 > gfc_integer_kinds[i3].bit_size) + { + gfc_error ("The absolute value of SHIFT at %L must be less " + "than or equal to BIT_SIZE('%s')", + &expr2->where, arg1); + return FAILURE; + } + } + if (or_equal) { if (i2 > gfc_integer_kinds[i3].bit_size) @@ -1927,6 +1943,9 @@ gfc_check_ishft (gfc_expr *i, gfc_expr *shift) || type_check (shift, 1, BT_INTEGER) == FAILURE) return FAILURE; + if (less_than_bitsize1 ("I", i, NULL, shift, true) == FAILURE) + return FAILURE; + return SUCCESS; } @@ -1938,7 +1957,35 @@ gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size) || type_check (shift, 1, BT_INTEGER) == FAILURE) return FAILURE; - if (size != NULL && type_check (size, 2, BT_INTEGER) == FAILURE) + if (size != NULL) + { + int i2, i3; + + if (type_check (size, 2, BT_INTEGER) == FAILURE) + return FAILURE; + + if (less_than_bitsize1 ("I", i, "SIZE", size, true) == FAILURE) + return FAILURE; + + gfc_extract_int (size, &i3); + if (i3 <= 0) + { + gfc_error ("SIZE at %L must be positive", &size->where); + return FAILURE; + } + + gfc_extract_int (shift, &i2); + if (i2 < 0) + i2 = -i2; + + if (i2 > i3) + { + gfc_error ("The absolute value of SHIFT at %L must be less than " + "or equal to SIZE at %L", &shift->where, &size->where); + return FAILURE; + } + } + else if (less_than_bitsize1 ("I", i, NULL, shift, true) == FAILURE) return FAILURE; return SUCCESS; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 5da63c43ca1..e5db96a1939 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,10 @@ 2011-10-20 Steven G. Kargl + PR fortran/50514 + * gfortran.dg/ishft_3.f90: Update test. + +2011-10-20 Steven G. Kargl + PR fortran/50524 * gfortran.dg/substring_integer_index.f90: New test. diff --git a/gcc/testsuite/gfortran.dg/ishft_3.f90 b/gcc/testsuite/gfortran.dg/ishft_3.f90 dissimilarity index 89% index fa3938ef9f0..626e71ce44e 100644 --- a/gcc/testsuite/gfortran.dg/ishft_3.f90 +++ b/gcc/testsuite/gfortran.dg/ishft_3.f90 @@ -1,11 +1,38 @@ -! { dg-do compile } -program ishft_3 - integer i, j - write(*,*) ishftc( 3, 2, 3 ) - write(*,*) ishftc( 3, 2, i ) - write(*,*) ishftc( 3, i, j ) - write(*,*) ishftc( 3, 128 ) ! { dg-error "exceeds BIT_SIZE of first" } - write(*,*) ishftc( 3, 0, 128 ) ! { dg-error "exceeds BIT_SIZE of first" } - write(*,*) ishftc( 3, 0, 0 ) ! { dg-error "Invalid third argument" } - write(*,*) ishftc( 3, 3, 2 ) ! { dg-error "exceeds third argument" } -end program +! { dg-do compile } +! PR fortran/50514 +program ishft_3 + + implicit none + + integer j, m + + m = 42 + ! + ! These should compile. + ! + j = ishft(m, 16) + j = ishft(m, -16) + j = ishftc(m, 16) + j = ishftc(m, -16) + ! + ! These should issue an error. + ! + j = ishft(m, 640) ! { dg-error "absolute value of SHIFT" } + j = ishftc(m, 640) ! { dg-error "absolute value of SHIFT" } + j = ishft(m, -640) ! { dg-error "absolute value of SHIFT" } + j = ishftc(m, -640) ! { dg-error "absolute value of SHIFT" } + + ! abs(SHIFT) must be <= SIZE + + j = ishftc(m, 1, 2) + j = ishftc(m, 1, 2) + j = ishftc(m, -1, 2) + j = ishftc(m, -1, 2) + + j = ishftc(m, 10, 2)! { dg-error "absolute value of SHIFT" } + j = ishftc(m, 10, 2)! { dg-error "absolute value of SHIFT" } + j = ishftc(m, -10, 2)! { dg-error "absolute value of SHIFT" } + j = ishftc(m, -10, 2)! { dg-error "absolute value of SHIFT" } + + j = ishftc(m, 1, -2) ! { dg-error "must be positive" } +end program -- 2.11.4.GIT