Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I had a bug in my code which was a result of insufficient validation of a string passed to the built-in hex function.

In my opinion, hex is much too forgiving. As shown in its documentation, it allows for a string to have a leading 0x; for example, it converts 0xC to 12. Although x is not a legal hexadecimal character, it is customary to denote a hex value with the 0xC prefix. This is quite reasonable.

Although not mentioned in the documentation, it also allows for a string to have a leading x; for example, it converts xC to 12. I guess that's reasonable, too.

My problem occurred when I inadvertently passed a lone x to hex. The function returned 0. Even with warnings enabled, I did not get a warning message (perl v5.12.2, linux):

perl -w -E "say hex(q{x})" 0

Passing it an illegal hex string like t does generate a warning, as desired:

perl -w -E "say hex(q{t})" Illegal hexadecimal digit 't' ignored at -e line 1. 0

I think passing a lone x should generate a warning message (with warnings enabled), but I'm not sure it's worth submitting a perlbug since I really need to do some checking before passing a string to hex anyway.

Here is a wrapper function I decided to use. It calls hex after performing some input validation:

sub hex2 { my $str = shift; $str =~ s/^(0x|x)//; if (length $str) { if ($str =~ /([^0-9a-f])/i) { die "hex2: Illegal hexadecimal digit found: '$1'"; } else { return hex $str; } } else { die "hex2: No chars found after stripping leading 0x or x"; } }

Another approach is to override hex.

In reply to Input validation for built-in hex function by toolic

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (3)
    As of 2018-03-18 05:00 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (228 votes). Check out past polls.