Skip to content

Commit cae9813

Browse files
committed
add incomplete pattern text
1 parent 541cfdf commit cae9813

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

project/fortran-regex.depend

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# depslib dependency file v1.0
2-
1671275921 source:/Users/federico/code/fortran-regex/src/regex.f90
2+
1671299845 source:/Users/federico/code/fortran-regex/src/regex.f90
33

44
1671276031 source:/Users/federico/code/fortran-regex/test/test_1.f90
55

src/regex.f90

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,9 @@ subroutine new_from_pattern(this,pattern)
474474

475475
i = i+1 ! Increment i to avoid including "^" in the char-buffer
476476

477-
! Check this is not an incomplete pattern
477+
! incomplete pattern
478478
if (i>=lenp) then
479-
stop 'incomplete pattern'
479+
call this%destroy()
480480
return
481481
end if
482482

@@ -491,17 +491,23 @@ subroutine new_from_pattern(this,pattern)
491491
i = i+loc
492492
if (DEBUG) print "('[regex] at end of multi-character pattern: ',a)", trim(ccl_buf)
493493
else
494-
stop 'incomplete [] pattern'
494+
! Incomplete [] pattern
495+
call this%destroy()
496+
return
495497
end if
496498

497499
! If there is any escape character(s), just check that the next is nonempty
498500
loc = index(ccl_buf,'\')
499501
if (loc>0) then
500502
if (loc>=len(ccl_buf)) then
501-
stop 'incomplete escaped character inside [] pattern'
503+
! stop 'incomplete escaped character inside [] pattern'
504+
call this%destroy()
505+
return
502506
end if
503507
if (ccl_buf(loc+1:loc+1)==SPACE) then
504-
stop 'empty escaped character inside [] pattern'
508+
! stop 'empty escaped character inside [] pattern'
509+
call this%destroy()
510+
return
505511
end if
506512
end if
507513

@@ -526,6 +532,7 @@ subroutine new_from_pattern(this,pattern)
526532

527533
! Save number of patterns
528534
this%n = j-1
535+
return
529536

530537
end subroutine new_from_pattern
531538

test/tests.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ program tests
2222
! Test #2
2323
call add_test(run_test2())
2424

25+
! Test #3
26+
call add_test(test_invalid())
27+
28+
if (nfailed<=0) then
29+
print *, 'SUCCESS! all tests passed.'
30+
stop 0
31+
else
32+
print *, 'ERROR: ',nfailed,' tests failed, ',npassed,' passed.'
33+
stop 1
34+
end if
35+
2536

2637

2738

@@ -36,6 +47,21 @@ subroutine add_test(successful_test)
3647
end if
3748
end subroutine add_test
3849

50+
! Test two bug patterns reported by @DavidKorczynski in https://github.com/kokke/tiny-regex-c/issues/44
51+
logical function test_invalid() result(success)
52+
53+
type(regex_op) :: re
54+
55+
! Test 1: inverted set without a closing ']'
56+
re = parse_pattern("\\\x01[^\\\xff][^")
57+
success = re%n==0; if (.not.success) return
58+
59+
! Test 1: inverted set without a closing ']'
60+
re = parse_pattern("\\\x01[^\\\xff][\\")
61+
success = re%n==0; if (.not.success) return
62+
63+
end function test_invalid
64+
3965

4066

4167

0 commit comments

Comments
 (0)