confirming/invalidating a few assumptions with different Awks/OSs

I’m trying to implement a robust ere_parenthesize function that requires to accurately parse the bracket expressions of a user-provided ERE.

The difficult part is that, while the support for character classes [: :], equivalence classes [= =] and collating symbols [. .] in bracket expressions differs between Awk implementations, it is critical for determining the termination of a bracket expression.

A simple example would be that /[[:punct:]]/ is equivalent to /[:[punct]]/ when Awk doesn’t support [: :].


I brainstormed a few runtime checks that are not enough to fully characterize a regex engine given the constraint that they shall not induce a crash. Still, I ran them with multiple Awks/OSs and made a few assumptions in light of the results:

  1. An implementation that supports [= =] but doesn’t support standard backslash-escape sequences within it always has the termination bug found here:

    match("]", /[[=x=]?]/) == 0 (support for equivalence classes)

    match("]", /[[=x]?]/) == 1 (termination bug)

    implies:

    match("]", /[[=t=]?]/) == 1 (no support for standard backslash-escape sequences within [= =])

  2. An implementation that supports [= =] and standard backslash-escape sequences within it does not have termination bugs:

    match("]", /[[=x=]?]/) == 0 (support for equivalence classes)

    match("t", /[[=t=]?]/) == 1 (support for standard backslash-escape sequences within [= =])

    implies:

    match("]", /[[=t]]/) (crash)

  3. An implementation that supports [: :] but doesn’t support [= =] always has termination bugs:

    match("1", /[[:xdigit:]]/) == 1 (support for character classes)

    match("]", /[[=x=]?]/) == 1 (no support for equivalence classes)

    implies:

    match("]", /[[:xdigit]?]/) == 1 (termination bug)

    match("]", /[[:abc:]?]/) == 1 (termination bug)

    match("]", /[[::]?]/) == 1 (termination bug)

    match("]", /[[:]?]/) == 1 (termination bug)


My question is about confirming/invalidating the above assumptions; could you provide the results of running the following code with the Awks/OSs that you have at hand?

awk 'BEGIN {
    ere_brackets_have_character_classes    = match("1", /[[:xdigit:]]/)
    ere_brackets_have_equivalence_classes  = !match("]", /[[=x=]?]/)
    ere_brackets_have_backslash_escape_bug = match("]", /[[=t=]?]/)

    print "ere_brackets_have_character_classes    :", ere_brackets_have_character_classes
    print "ere_brackets_have_equivalence_classes  :", ere_brackets_have_equivalence_classes
    print "ere_brackets_have_backslash_escape_bug :", ere_brackets_have_backslash_escape_bug

    if (ere_brackets_have_equivalence_classes) {
        if (ere_brackets_have_backslash_escape_bug) {
            print "Assumption #1: expected output: 1"
            r = "[[=x]?]"
            print match("]", r)
        } else {
            print "Assumption #2: expected output: crash"
            r = "[[=\t]]"            
            match("]", r)
        }
    } else if (ere_brackets_have_character_classes) {
        print "Assumption #1: expected output: 1"
        split("[[:xdigit]?] [[:abc:]?] [[::]?] [[:]?]", a, " ")
        print match("]", a[1]) && 
              match("]", a[2]) && 
              match("]", a[3]) && 
              match("]", a[4])
    }
    else {
        print "no expected output: nothing"
    }
}'

note: Some Awks compile the EREs before running the code when they are provided as string constants or within / /; as a workaround I stored them in variables.


ASIDE

match("1", /[[:xdigit:]]/) should be locale independent, am I right?

FreeBSD 10.3-RELEASE-p7:

awk version 20121220 (FreeBSD)

ere_brackets_have_character_classes    : 1
ere_brackets_have_equivalence_classes  : 0
ere_brackets_have_backslash_escape_bug : 1
Assumption #1: expected output: 1
1

Ubuntu 22.04.4:

busybox → 1:1.30.1-7ubuntu3

ere_brackets_have_character_classes    : 1
ere_brackets_have_equivalence_classes  : 1
ere_brackets_have_backslash_escape_bug : 0
Assumption #2: expected output: crash
awk: bad regex '[[=t]]': Unmatched [, [^, [:, [., or [=

original-awk → 2018-08-27-1

ere_brackets_have_character_classes    : 1
ere_brackets_have_equivalence_classes  : 0
ere_brackets_have_backslash_escape_bug : 1
Assumption #1: expected output: 1
1

mawk → 1.3.4.20200120-3

ere_brackets_have_character_classes    : 1
ere_brackets_have_equivalence_classes  : 0
ere_brackets_have_backslash_escape_bug : 1
Assumption #1: expected output: 1
mawk: run time error: regular expression compile failed (bad class -- [], [^] or [)
[[:xdigit]?]
    FILENAME="" FNR=0 NR=0

Debian 8.11:

mawk → 1.3.3-17

ere_brackets_have_character_classes    : 0
ere_brackets_have_equivalence_classes  : 0
ere_brackets_have_backslash_escape_bug : 1
no expected output: nothing

5

$ awk --version
GNU Awk 5.3.0, API 4.0, PMA Avon 8-g1, (GNU MPFR 4.2.1, GNU MP 6.3.0)
Copyright (C) 1989, 1991-2023 Free Software Foundation.

------
awk --traditional -f script.awk
ere_brackets_have_character_classes    : 1
ere_brackets_have_equivalence_classes  : 1
ere_brackets_have_backslash_escape_bug : 0
Assumption #2: expected output: crash
awk: cmd. line:18: fatal: invalid regexp: Unmatched [, [^, [:, [., or [=: /[[=t]]/

------
awk --posix -f script.awk
ere_brackets_have_character_classes    : 1
ere_brackets_have_equivalence_classes  : 1
ere_brackets_have_backslash_escape_bug : 0
Assumption #2: expected output: crash
awk: cmd. line:18: fatal: invalid regexp: Unmatched [, [^, [:, [., or [=: /[[=t]]/

------
awk -f script.awk
ere_brackets_have_character_classes    : 1
ere_brackets_have_equivalence_classes  : 1
ere_brackets_have_backslash_escape_bug : 0
Assumption #2: expected output: crash
awk: cmd. line:18: fatal: invalid regexp: Unmatched [, [^, [:, [., or [=: /[[=t]]/

Regarding:

match("1", /[[:xdigit:]]/) should be locale independent, am I right?

That depends what you mean by locale independent. It’s locale independent in as much as [:xdigit:] will match whichever characters are considered valid hex digits in your locale, but then that means it can match different characters in different locales which makes it’s result given any specific input set locale dependent. So if there’s some locale out there that uses the batman symbol to mean the hex digit 1 instead of the ascii character 1 then your match("1", /[[:xdigit:]]/) would fail to recognize it. If you want your code to be portable with respect to ascii input, you should set LC_ALL=C or LC_ALL=POSIX before it runs.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật