Skip to content

Commit b31745f

Browse files
committed
PHPC-1230: Refactor hardcoded ObjectID lenghts
1 parent aed8384 commit b31745f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/BSON/ObjectId.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include "phongo_compat.h"
3131
#include "php_phongo.h"
3232

33+
#define PHONGO_OID_SIZE sizeof(((php_phongo_objectid_t *)0)->oid)
34+
#define PHONGO_OID_LEN (PHONGO_OID_SIZE - 1)
35+
3336
zend_class_entry* php_phongo_objectid_ce;
3437

3538
/* Initialize the object with a generated value and return whether it was
@@ -163,7 +166,7 @@ static PHP_METHOD(ObjectId, __toString)
163166
return;
164167
}
165168

166-
PHONGO_RETURN_STRINGL(intern->oid, 24);
169+
PHONGO_RETURN_STRINGL(intern->oid, PHONGO_OID_LEN);
167170
} /* }}} */
168171

169172
/* {{{ proto array MongoDB\BSON\ObjectId::jsonSerialize()
@@ -179,7 +182,7 @@ static PHP_METHOD(ObjectId, jsonSerialize)
179182
intern = Z_OBJECTID_OBJ_P(getThis());
180183

181184
array_init_size(return_value, 1);
182-
ADD_ASSOC_STRINGL(return_value, "$oid", intern->oid, 24);
185+
ADD_ASSOC_STRINGL(return_value, "$oid", intern->oid, PHONGO_OID_LEN);
183186
} /* }}} */
184187

185188
/* {{{ proto string MongoDB\BSON\ObjectId::serialize()
@@ -199,11 +202,11 @@ static PHP_METHOD(ObjectId, serialize)
199202

200203
#if PHP_VERSION_ID >= 70000
201204
array_init_size(&retval, 1);
202-
ADD_ASSOC_STRINGL(&retval, "oid", intern->oid, 24);
205+
ADD_ASSOC_STRINGL(&retval, "oid", intern->oid, PHONGO_OID_LEN);
203206
#else
204207
ALLOC_INIT_ZVAL(retval);
205208
array_init_size(retval, 1);
206-
ADD_ASSOC_STRINGL(retval, "oid", intern->oid, 24);
209+
ADD_ASSOC_STRINGL(retval, "oid", intern->oid, PHONGO_OID_LEN);
207210
#endif
208211

209212
PHP_VAR_SERIALIZE_INIT(var_hash);
@@ -358,7 +361,7 @@ static phongo_create_object_retval php_phongo_objectid_clone_object(zval* object
358361
#endif
359362

360363
// Use memcpy to copy bson value to avoid converting to string and back
361-
memcpy(&new_intern->oid, &intern->oid, 25);
364+
memcpy(&new_intern->oid, &intern->oid, PHONGO_OID_SIZE);
362365
new_intern->initialized = true;
363366

364367
return new_object;

0 commit comments

Comments
 (0)