Skip to content

Commit

Permalink
Php::Value objects could crash if comparing non-string values with st…
Browse files Browse the repository at this point in the history
…rings, fixes #524
  • Loading branch information
EmielBruijntjes committed Apr 20, 2024
1 parent f9fe7a9 commit 3cee025
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
19 changes: 13 additions & 6 deletions include/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,12 @@ class PHPCPP_EXPORT Value : private HashParent
* Comparison operators for hardcoded strings
* @param value
*/
bool operator==(const char *value) const { return ::strcmp(rawValue(), value) == 0; }
bool operator!=(const char *value) const { return ::strcmp(rawValue(), value) != 0; }
bool operator<=(const char *value) const { return ::strcmp(rawValue(), value) <= 0; }
bool operator>=(const char *value) const { return ::strcmp(rawValue(), value) >= 0; }
bool operator< (const char *value) const { return ::strcmp(rawValue(), value) < 0; }
bool operator> (const char *value) const { return ::strcmp(rawValue(), value) > 0; }
bool operator==(const char *value) const { return strcmp(value) == 0; }
bool operator!=(const char *value) const { return strcmp(value) != 0; }
bool operator<=(const char *value) const { return strcmp(value) <= 0; }
bool operator>=(const char *value) const { return strcmp(value) >= 0; }
bool operator< (const char *value) const { return strcmp(value) < 0; }
bool operator> (const char *value) const { return strcmp(value) > 0; }

/**
* Comparison operators for hardcoded Value
Expand Down Expand Up @@ -424,6 +424,13 @@ class PHPCPP_EXPORT Value : private HashParent
*/
const char *rawValue() const;

/**
* Helper function for string comparison
* @param value
* @return int
*/
int strcmp(const char *value) const;

/**
* Retrieve the value as number
*
Expand Down
27 changes: 27 additions & 0 deletions zend/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,33 @@ const char *Value::rawValue() const
return nullptr;
}

/**
* Helper function for string comparison
* @param value
* @return int
*/
int Value::strcmp(const char *value) const
{
// we need the string representation
zend_string *s = zval_get_string(_val);

// remember size of the two strings
size_t valuelen = ::strlen(value);
size_t slen = ZSTR_LEN(s);

// get the result for comparing the initial, overlapping, bytes
auto result = strncmp(ZSTR_VAL(s), value, std::min(valuelen, slen));

// we no longer need the string
zend_string_release(s);

// if there are differences, we can expose thosw
if (result != 0) return result;

// the shorter string comes first
return slen - valuelen;
}

/**
* Retrieve the value as decimal
* @return double
Expand Down

0 comments on commit 3cee025

Please sign in to comment.