From patchwork Sat Oct 15 21:21:10 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Kargl X-Patchwork-Id: 119995 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 04228B6F9F for ; Sun, 16 Oct 2011 08:21:31 +1100 (EST) Received: (qmail 11042 invoked by alias); 15 Oct 2011 21:21:28 -0000 Received: (qmail 11027 invoked by uid 22791); 15 Oct 2011 21:21:27 -0000 X-SWARE-Spam-Status: No, hits=-2.2 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, TW_HF X-Spam-Check-By: sourceware.org Received: from troutmask.apl.washington.edu (HELO troutmask.apl.washington.edu) (128.95.76.21) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 15 Oct 2011 21:21:11 +0000 Received: from troutmask.apl.washington.edu (localhost.apl.washington.edu [127.0.0.1]) by troutmask.apl.washington.edu (8.14.5/8.14.5) with ESMTP id p9FLLBlM038901; Sat, 15 Oct 2011 14:21:11 -0700 (PDT) (envelope-from sgk@troutmask.apl.washington.edu) Received: (from sgk@localhost) by troutmask.apl.washington.edu (8.14.5/8.14.5/Submit) id p9FLLAWO038900; Sat, 15 Oct 2011 14:21:10 -0700 (PDT) (envelope-from sgk) Date: Sat, 15 Oct 2011 14:21:10 -0700 From: Steve Kargl To: fortran@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] fortran/50514 -- Fix static chekcing of ISHFT[C] arguments. Message-ID: <20111015212110.GA38875@troutmask.apl.washington.edu> Mime-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.4.2.3i Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This patch fixes the checking of constant SHIFT values against the bitsize of the argument to be shifted. I've been running this patch on amd64-*-freebsd for sevearl days without a problem (ie., multiple 'gmake check-gfortran' invocations). OK for trunk? 2011-10-15 Steven G. Kargl * gfortran.dg/ishft_3.f90: Update test. 2011-10-15 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. Index: testsuite/gfortran.dg/ishft_3.f90 =================================================================== --- testsuite/gfortran.dg/ishft_3.f90 (revision 179208) +++ testsuite/gfortran.dg/ishft_3.f90 (working copy) @@ -1,11 +1,38 @@ ! { dg-do compile } +! PR fortran/50514 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" } + + 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 Index: fortran/check.c =================================================================== --- fortran/check.c (revision 179208) +++ fortran/check.c (working copy) @@ -318,6 +318,22 @@ less_than_bitsize1 (const char *arg1, gf { gfc_extract_int (expr2, &i2); i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false); + + /* For ISHFT[C], |shift| <= bit_size(i). */ + if (strncmp (arg2, "ISHFT", 5) == 0) + { + 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) @@ -1961,6 +1977,9 @@ gfc_check_ishft (gfc_expr *i, gfc_expr * || type_check (shift, 1, BT_INTEGER) == FAILURE) return FAILURE; + if (less_than_bitsize1 ("I", i, "ISHFT", shift, true) == FAILURE) + return FAILURE; + return SUCCESS; } @@ -1972,7 +1991,35 @@ gfc_check_ishftc (gfc_expr *i, gfc_expr || 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, "ISHFTC", shift, true) == FAILURE) return FAILURE; return SUCCESS;