is_hexadecimal recognizes non hex-strings as hexadecimal

Post general support questions here that do not specifically fall into the Linux or Windows categories.

Moderators: Developers, Moderators

Post Reply
DkSoul
Posts: 2
Joined: Sun Jul 29, 2012 11:02 am

is_hexadecimal recognizes non hex-strings as hexadecimal

Post by DkSoul »

Hello,

I'm having problems with the output values returned by some scripts because cacti is interpreting them as hexadecimal values.

Example:

is_hex.php

Code: Select all

<?php
function is_hexadecimal($hexstr) {
        $hexstr = trim($hexstr);
        $i      = 0;
        $length = strlen($hexstr);
        while ($i < $length) {
                $part = substr($hexstr,$i,2);
                $i += 2;

                if (!preg_match('/[a-fA-F0-9]/', $part)) {
                        return false;
                } elseif ($i < $length) {
                        if (substr($hexstr,$i,1) != ":") {
                                return false;
                        }elseif ($i + 1 == $length) {
                                return false;
                        }
                        $i++;
                }else{
                        $i++;
                }
        }

        return true;
}
if ( is_hexadecimal($_SERVER["argv"][1]) ) {
  echo "Is hexadecimal!\n";
} else {
  echo "Not hexadecimal!\n";
}
?>

Code: Select all

$ php is_hex.php "jc:1"
Is hexadecimal!

Code: Select all

$ php is_hex.php "zf:5y:2"
Is hexadecimal!
I would like to know what are/should be the rules for an hex-string so that I can either rewrite this function or change the scripts.

Thank you!

PS: The function is_hexadecimal is inside cacti/site/lib/functions.php
PS2: Fixed this by replacing the regex with ^[a-fA-F0-9]{2}$. I would still like to know if this is really a bug or if by changing this I'm messing with something else..
User avatar
gandalf
Developer
Posts: 22383
Joined: Thu Dec 02, 2004 2:46 am
Location: Muenster, Germany
Contact:

Re: is_hexadecimal recognizes non hex-strings as hexadecimal

Post by gandalf »

Good find. Current code is obviously broken. Personally, I do prefer a different solution. But I will have to test that performance-wise.
R.
DkSoul
Posts: 2
Joined: Sun Jul 29, 2012 11:02 am

Re: is_hexadecimal recognizes non hex-strings as hexadecimal

Post by DkSoul »

gandalf wrote:Good find. Current code is obviously broken. Personally, I do prefer a different solution. But I will have to test that performance-wise.
R.
So far I've not seen any side effects from changing the regex, so I'm going to leave it like that.
Thank you for your feedback gandalf!
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests