Subject: Re: [bug#67841] [PATCH] Clarify error messages for misuse of m4_warn and --help for -W.

From: Jacob Bachmeyer <[email-masked]>
Date: Fri, 15 Dec 2023 18:08:00 -0600

Zack Weinberg wrote:
> [...]
>
> Since this touches code shared between Autoconf and Automake, I’m not
> checking it in yet and I’m requesting comments from both sides of the
> fence.  Also, there’s a perl 2.14ism in one place (s///a) which I need
> to figure out how to make 2.6-compatible before it can land.
>
> [...]
>  
> +sub report_bad_channel ($$)
> +{
> +  my ($channel, $location) = @_;
> +  my $message;
> +  my $report_as = 'error';
> +
> +  # quotemeta is both too aggressive (e.g. it escapes '-') and
> +  # too generous (it turns control characters into \ + themselves,
> +  # not into symbolic escapes).
> +  my $q_channel = $channel;
> +  $q_channel =~ s/\\/\\\\/g;
> +  $q_channel =~ s/([^\x20-\x7e])/"\\x".sprintf("%02x", ord($1))/aeg;
> +  $q_channel =~ s/(?=[\"\$\'\@\`])/\\/g;
> +  $q_channel = '"' . $q_channel . '"';
>
> [...]
>   

If I am reading perlre correctly, you should be able to simply drop the 
/a modifier because it has no effect on the pattern you have written, 
since you are using an explicit character class and are *not* using the 
/i modifier.  (The documentation says that /a only affects the \d, \s, 
\w Perl character classes and the POSIX character classes.)  Further, 
Perl's sprintf is indeed sprintf, so you should be able to rewrite that 
substitution as:

s/([^\x20-\x7e])/sprintf('\\x%02x', ord $1)/eg

This slightly simplifies the program (eliminating one concatenation), 
which is probably of trivial consequence, since this code is on an error 
path and not an inner loop.  More importantly, including the "\\x" in 
the sprintf format string makes the intended output more clear.  The 
single quotes are both a (very slight) performance optimization and 
serve to document that no direct interpolation is intended because this 
is a format string.  Perl's handling of backslash in single quotes is 
fun:  backslash escapes itself but will also be taken literally if it 
does not form an escape that is allowed in q[].

-- Jacob