From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1QAEWB-0001VL-Vq for garchives@archives.gentoo.org; Thu, 14 Apr 2011 04:51:05 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 9737B1C027; Thu, 14 Apr 2011 04:50:16 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 4FD1C1C024 for ; Thu, 14 Apr 2011 04:50:16 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id A97941B415C for ; Thu, 14 Apr 2011 04:50:15 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id EDE8080065 for ; Thu, 14 Apr 2011 04:50:14 +0000 (UTC) From: "Petteri Räty" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Petteri Räty" Message-ID: <5be695e3dd1739ff776e57a6ccb425777979d3c0.betelgeuse@gentoo> Subject: [gentoo-commits] proj/libbash:master commit in: bashast/, test/ X-VCS-Repository: proj/libbash X-VCS-Files: bashast/libbashWalker.g test/walker_test.cpp X-VCS-Directories: bashast/ test/ X-VCS-Committer: betelgeuse X-VCS-Committer-Name: Petteri Räty X-VCS-Revision: 5be695e3dd1739ff776e57a6ccb425777979d3c0 Date: Thu, 14 Apr 2011 04:50:14 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 9ff232e86972c43a85675d26a322d81a commit: 5be695e3dd1739ff776e57a6ccb425777979d3c0 Author: Mu Qiao gentoo org> AuthorDate: Thu Apr 14 01:09:28 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Thu Apr 14 01:09:28 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3D5be695e3 Raise exception if array index is less than 0 An interpreter_exception will be thrown if index is less than 0. --- bashast/libbashWalker.g | 20 +++++++++++++++++--- test/walker_test.cpp | 13 +++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g index 43304f1..b5e9974 100644 --- a/bashast/libbashWalker.g +++ b/bashast/libbashWalker.g @@ -34,6 +34,7 @@ void set_interpreter(std::shared_ptr w); } @postinclude{ #include "core/interpreter.h" +#include static std::shared_ptr walker; void set_interpreter(std::shared_ptr w) { @@ -41,6 +42,17 @@ void set_interpreter(std::shared_ptr w) } } =20 +@members{ + +inline void set_index(const std::string& name, unsigned& index, int valu= e) +{ + if(value < 0) + throw interpreter_exception((boost::format("Array index is less than 0= : \%s[\%d]") \% name \% value).str()); + index =3D value; +} + +} + start: list|EOF; =20 list: ^(LIST var_def+); @@ -55,7 +67,7 @@ name returns[std::string libbash_value, unsigned index] $index =3D 0; }: ^(libbash_name=3Dname_base value=3Darithmetics){ - $index =3D value; + set_index(libbash_name, $index, value); $libbash_value =3D libbash_name; } |libbash_name=3Dname_base{ @@ -70,7 +82,7 @@ options{ k=3D1; }: var_def @declarations { std::map values; - int index =3D 0; + unsigned index =3D 0; bool is_null =3D true; }: ^(EQUALS name (libbash_string=3Dstring_expr { is_null =3D false; })?){ @@ -78,7 +90,9 @@ var_def } |^(EQUALS libbash_name=3Dname_base ^(ARRAY ( (libbash_string=3Dstring_expr - |^(EQUALS value=3Darithmetics { index =3D value; } libbash_str= ing=3Dstring_expr)) + |^(EQUALS value=3Darithmetics { + set_index(libbash_name, index, value); + } libbash_string=3Dstring_expr)) { values[index++] =3D libbash_string; })* )){ walker->define(libbash_name, values); diff --git a/test/walker_test.cpp b/test/walker_test.cpp index 8185710..6f13176 100644 --- a/test/walker_test.cpp +++ b/test/walker_test.cpp @@ -59,3 +59,16 @@ TEST_STRING_ASSIGNMENT(str_assignment6, "str=3D\"/ \n \r\n & && ||| || > < ' : ; , ( (( )= )) ;; { } >=3D <=3D\"", "str", "/ \n \r\n & && ||| || > < ' : ; , ( (( ) )) ;; {= } >=3D <=3D") + +TEST(array_index, out_of_bound) +{ + std::string script =3D "a[-1]=3D\"1\""; + std::istringstream input(script); + parser_builder pbuilder(input); + EXPECT_THROW(pbuilder.create_walker_builder(), interpreter_exception); + + std::string script2 =3D "a=3D(1 2 [-5]=3D1)"; + std::istringstream input2(script2); + parser_builder pbuilder2(input2); + EXPECT_THROW(pbuilder2.create_walker_builder(), interpreter_exception)= ; +}