Skip to content

Commit b986a77

Browse files
committed
Track Azure failure.
1 parent 8be3f00 commit b986a77

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

ext/hash/hash.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ PHP_HASH_API int php_hash_serialize_spec(const php_hashcontext_object *hash, zen
182182
size_t pos = 0, sz, count;
183183
unsigned char *buf = (unsigned char *) hash->context;
184184
zval tmp;
185-
*magic = 2;
185+
*magic = PHP_HASH_SERIALIZE_MAGIC_SPEC;
186186
array_init(zv);
187187
while (*spec != '\0' && *spec != '.') {
188188
char specch = *spec;
@@ -222,21 +222,21 @@ PHP_HASH_API int php_hash_unserialize_spec(php_hashcontext_object *hash, zend_lo
222222
size_t pos = 0, sz, count, j = 0;
223223
unsigned char *buf = (unsigned char *) hash->context;
224224
zval *elt;
225-
if (magic != 2 || Z_TYPE_P(zv) != IS_ARRAY) {
226-
return FAILURE;
225+
if (magic != PHP_HASH_SERIALIZE_MAGIC_SPEC || Z_TYPE_P(zv) != IS_ARRAY) {
226+
return -2000;
227227
}
228228
while (*spec != '\0' && *spec != '.') {
229229
char specch = *spec;
230230
count = parse_serialize_spec(&spec, &pos, &sz);
231231
if (pos + count * sz > hash->ops->context_size) {
232-
return FAILURE;
232+
return -1000 - pos;
233233
}
234234
if (specch == '-') {
235235
pos += count;
236236
} else if (sz == 1 && count > 1) {
237237
elt = zend_hash_index_find(Z_ARRVAL_P(zv), j);
238238
if (!elt || Z_TYPE_P(elt) != IS_STRING || Z_STRLEN_P(elt) != count) {
239-
return FAILURE;
239+
return -1000 - pos;
240240
}
241241
++j;
242242
memcpy(buf + pos, Z_STRVAL_P(elt), count);
@@ -246,14 +246,14 @@ PHP_HASH_API int php_hash_unserialize_spec(php_hashcontext_object *hash, zend_lo
246246
uint64_t val;
247247
elt = zend_hash_index_find(Z_ARRVAL_P(zv), j);
248248
if (!elt || Z_TYPE_P(elt) != IS_LONG) {
249-
return FAILURE;
249+
return -1000 - pos;
250250
}
251251
++j;
252252
val = (uint32_t) zval_get_long(elt);
253253
if (sz == 8) {
254254
elt = zend_hash_index_find(Z_ARRVAL_P(zv), j);
255255
if (!elt || Z_TYPE_P(elt) != IS_LONG) {
256-
return FAILURE;
256+
return -1000 - pos;
257257
}
258258
++j;
259259
val += ((uint64_t) zval_get_long(elt)) << 32;
@@ -265,7 +265,7 @@ PHP_HASH_API int php_hash_unserialize_spec(php_hashcontext_object *hash, zend_lo
265265
}
266266
}
267267
if (*spec == '.' && pos != hash->ops->context_size) {
268-
return FAILURE;
268+
return -2001;
269269
}
270270
return SUCCESS;
271271
}
@@ -1434,6 +1434,7 @@ PHP_METHOD(HashContext, __unserialize)
14341434
HashTable *data;
14351435
zval *algo_zv, *magic_zv, *options_zv, *hash_zv, *members_zv;
14361436
zend_long magic, options;
1437+
int unserialize_result;
14371438
const php_hash_ops *ops;
14381439

14391440
if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
@@ -1481,8 +1482,9 @@ PHP_METHOD(HashContext, __unserialize)
14811482
ops->hash_init(hash->context);
14821483
hash->options = options;
14831484

1484-
if (ops->hash_unserialize(hash, magic, hash_zv) != SUCCESS) {
1485-
zend_value_error("HashContext for algorithm '%s' cannot be unserialized, format may be non-portable", ops->algo);
1485+
unserialize_result = ops->hash_unserialize(hash, magic, hash_zv);
1486+
if (unserialize_result != SUCCESS) {
1487+
zend_value_error("HashContext for algorithm '%s' cannot be unserialized, format may be non-portable (code %d)", ops->algo, unserialize_result);
14861488
/* Free internally allocated resources */
14871489
php_hashcontext_dtor(Z_OBJ_P(object));
14881490
RETURN_THROWS();

ext/hash/php_hash.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525

2626
#define PHP_HASH_HMAC 0x0001
2727

28+
#define PHP_HASH_SERIALIZE_MAGIC_LITTLEENDIAN 0
29+
#define PHP_HASH_SERIALIZE_MAGIC_BIGENDIAN 1
30+
#define PHP_HASH_SERIALIZE_MAGIC_SPEC 2
2831
#ifdef WORDS_BIGENDIAN
29-
#define PHP_HASH_SERIALIZE_MAGIC 1
32+
#define PHP_HASH_SERIALIZE_MAGIC PHP_HASH_SERIALIZE_MAGIC_BIGENDIAN
3033
#else
31-
#define PHP_HASH_SERIALIZE_MAGIC 0
34+
#define PHP_HASH_SERIALIZE_MAGIC PHP_HASH_SERIALIZE_MAGIC_LITTLEENDIAN
3235
#endif
3336

3437
#define L64 INT64_C

ext/hash/tests/hash_serialize_003.phpt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,16 @@ foreach ($serializations as $slist) {
220220
$serial = base64_encode(serialize($ctx));
221221

222222
for ($i = 1; $i < count($slist); ++$i) {
223-
$ctx = unserialize(base64_decode($slist[$i]));
224-
hash_update($ctx, "Can’t tell if this is true or dream");
225-
$hash2 = hash_final($ctx);
226-
if ($hash !== $hash2) {
227-
echo "$algo: unexpected hash $hash2 for serialization {$slist[$i]}\n";
223+
try {
224+
$ctx = unserialize(base64_decode($slist[$i]));
225+
hash_update($ctx, "Can’t tell if this is true or dream");
226+
$hash2 = hash_final($ctx);
227+
if ($hash !== $hash2) {
228+
echo "$algo: unexpected hash $hash2 for serialization {$slist[$i]}\n";
229+
}
230+
} catch (Error $e) {
231+
echo "$algo: problem with serialization {$slist[$i]}\n";
232+
echo " ", $e->getMessage(), "\n", $e->getTraceAsString();
228233
}
229234
}
230235
}

0 commit comments

Comments
 (0)