Skip to content

Commit 4f7f14d

Browse files
authored
PHPC-2168: Use consistent int types for APM fields and snprintf (#1383)
The buffer for snprintf is slightly larger than necessary (21 bytes), but this is consistent with the buffer size we use elsewhere.
1 parent b83ab98 commit 4f7f14d

File tree

4 files changed

+37
-37
lines changed

4 files changed

+37
-37
lines changed

src/MongoDB/Monitoring/CommandFailedEvent.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getError)
7272
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getOperationId)
7373
{
7474
php_phongo_commandfailedevent_t* intern;
75-
char int_as_string[20];
75+
char operation_id[24];
7676

7777
intern = Z_COMMANDFAILEDEVENT_OBJ_P(getThis());
7878

7979
PHONGO_PARSE_PARAMETERS_NONE();
8080

81-
sprintf(int_as_string, "%" PRIu64, intern->operation_id);
82-
RETVAL_STRING(int_as_string);
81+
snprintf(operation_id, sizeof(operation_id), "%" PRId64, intern->operation_id);
82+
RETVAL_STRING(operation_id);
8383
}
8484

8585
/* Returns the reply document associated with the event */
@@ -106,14 +106,14 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getReply)
106106
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandFailedEvent, getRequestId)
107107
{
108108
php_phongo_commandfailedevent_t* intern;
109-
char int_as_string[20];
109+
char request_id[24];
110110

111111
intern = Z_COMMANDFAILEDEVENT_OBJ_P(getThis());
112112

113113
PHONGO_PARSE_PARAMETERS_NONE();
114114

115-
sprintf(int_as_string, "%" PRIu64, intern->request_id);
116-
RETVAL_STRING(int_as_string);
115+
snprintf(request_id, sizeof(request_id), "%" PRId64, intern->request_id);
116+
RETVAL_STRING(request_id);
117117
}
118118

119119
/* Returns the Server from which the event originated */
@@ -205,7 +205,7 @@ static HashTable* php_phongo_commandfailedevent_get_debug_info(phongo_compat_obj
205205
{
206206
php_phongo_commandfailedevent_t* intern;
207207
zval retval = ZVAL_STATIC_INIT;
208-
char operation_id[20], request_id[20];
208+
char operation_id[24], request_id[24];
209209
php_phongo_bson_state reply_state;
210210

211211
PHONGO_BSON_INIT_STATE(reply_state);
@@ -215,12 +215,12 @@ static HashTable* php_phongo_commandfailedevent_get_debug_info(phongo_compat_obj
215215
array_init_size(&retval, 6);
216216

217217
ADD_ASSOC_STRING(&retval, "commandName", intern->command_name);
218-
ADD_ASSOC_INT64(&retval, "durationMicros", (int64_t) intern->duration_micros);
218+
ADD_ASSOC_INT64(&retval, "durationMicros", intern->duration_micros);
219219

220220
ADD_ASSOC_ZVAL_EX(&retval, "error", &intern->z_error);
221221
Z_ADDREF(intern->z_error);
222222

223-
sprintf(operation_id, "%" PRIu64, intern->operation_id);
223+
snprintf(operation_id, sizeof(operation_id), "%" PRId64, intern->operation_id);
224224
ADD_ASSOC_STRING(&retval, "operationId", operation_id);
225225

226226
if (!php_phongo_bson_to_zval_ex(intern->reply, &reply_state)) {
@@ -230,7 +230,7 @@ static HashTable* php_phongo_commandfailedevent_get_debug_info(phongo_compat_obj
230230

231231
ADD_ASSOC_ZVAL(&retval, "reply", &reply_state.zchild);
232232

233-
sprintf(request_id, "%" PRIu64, intern->request_id);
233+
snprintf(request_id, sizeof(request_id), "%" PRId64, intern->request_id);
234234
ADD_ASSOC_STRING(&retval, "requestId", request_id);
235235

236236
{

src/MongoDB/Monitoring/CommandStartedEvent.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,28 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_CommandStartedEvent, getDatabaseName
8080
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandStartedEvent, getOperationId)
8181
{
8282
php_phongo_commandstartedevent_t* intern;
83-
char int_as_string[20];
83+
char operation_id[24];
8484

8585
intern = Z_COMMANDSTARTEDEVENT_OBJ_P(getThis());
8686

8787
PHONGO_PARSE_PARAMETERS_NONE();
8888

89-
sprintf(int_as_string, "%" PRIu64, intern->operation_id);
90-
RETVAL_STRING(int_as_string);
89+
snprintf(operation_id, sizeof(operation_id), "%" PRId64, intern->operation_id);
90+
RETVAL_STRING(operation_id);
9191
}
9292

9393
/* Returns the event's request ID */
9494
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandStartedEvent, getRequestId)
9595
{
9696
php_phongo_commandstartedevent_t* intern;
97-
char int_as_string[20];
97+
char request_id[24];
9898

9999
intern = Z_COMMANDSTARTEDEVENT_OBJ_P(getThis());
100100

101101
PHONGO_PARSE_PARAMETERS_NONE();
102102

103-
sprintf(int_as_string, "%" PRIu64, intern->request_id);
104-
RETVAL_STRING(int_as_string);
103+
snprintf(request_id, sizeof(request_id), "%" PRId64, intern->request_id);
104+
RETVAL_STRING(request_id);
105105
}
106106

107107
/* Returns the Server from which the event originated */
@@ -193,7 +193,7 @@ static HashTable* php_phongo_commandstartedevent_get_debug_info(phongo_compat_ob
193193
{
194194
php_phongo_commandstartedevent_t* intern;
195195
zval retval = ZVAL_STATIC_INIT;
196-
char operation_id[20], request_id[20];
196+
char operation_id[24], request_id[24];
197197
php_phongo_bson_state command_state;
198198

199199
PHONGO_BSON_INIT_STATE(command_state);
@@ -212,10 +212,10 @@ static HashTable* php_phongo_commandstartedevent_get_debug_info(phongo_compat_ob
212212
ADD_ASSOC_STRING(&retval, "commandName", intern->command_name);
213213
ADD_ASSOC_STRING(&retval, "databaseName", intern->database_name);
214214

215-
sprintf(operation_id, "%" PRIu64, intern->operation_id);
215+
snprintf(operation_id, sizeof(operation_id), "%" PRId64, intern->operation_id);
216216
ADD_ASSOC_STRING(&retval, "operationId", operation_id);
217217

218-
sprintf(request_id, "%" PRIu64, intern->request_id);
218+
snprintf(request_id, sizeof(request_id), "%" PRId64, intern->request_id);
219219
ADD_ASSOC_STRING(&retval, "requestId", request_id);
220220

221221
{

src/MongoDB/Monitoring/CommandSucceededEvent.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_CommandSucceededEvent, getDurationMi
6060
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandSucceededEvent, getOperationId)
6161
{
6262
php_phongo_commandsucceededevent_t* intern;
63-
char int_as_string[20];
63+
char operation_id[24];
6464

6565
intern = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(getThis());
6666

6767
PHONGO_PARSE_PARAMETERS_NONE();
6868

69-
sprintf(int_as_string, "%" PRIu64, intern->operation_id);
70-
RETVAL_STRING(int_as_string);
69+
snprintf(operation_id, sizeof(operation_id), "%" PRId64, intern->operation_id);
70+
RETVAL_STRING(operation_id);
7171
}
7272

7373
/* Returns the reply document associated with the event */
@@ -94,14 +94,14 @@ static PHP_METHOD(MongoDB_Driver_Monitoring_CommandSucceededEvent, getReply)
9494
static PHP_METHOD(MongoDB_Driver_Monitoring_CommandSucceededEvent, getRequestId)
9595
{
9696
php_phongo_commandsucceededevent_t* intern;
97-
char int_as_string[20];
97+
char request_id[24];
9898

9999
intern = Z_COMMANDSUCCEEDEDEVENT_OBJ_P(getThis());
100100

101101
PHONGO_PARSE_PARAMETERS_NONE();
102102

103-
sprintf(int_as_string, "%" PRIu64, intern->request_id);
104-
RETVAL_STRING(int_as_string);
103+
snprintf(request_id, sizeof(request_id), "%" PRId64, intern->request_id);
104+
RETVAL_STRING(request_id);
105105
}
106106

107107
/* Returns the Server from which the event originated */
@@ -189,7 +189,7 @@ static HashTable* php_phongo_commandsucceededevent_get_debug_info(phongo_compat_
189189
{
190190
php_phongo_commandsucceededevent_t* intern;
191191
zval retval = ZVAL_STATIC_INIT;
192-
char operation_id[20], request_id[20];
192+
char operation_id[24], request_id[24];
193193
php_phongo_bson_state reply_state;
194194

195195
PHONGO_BSON_INIT_STATE(reply_state);
@@ -199,9 +199,9 @@ static HashTable* php_phongo_commandsucceededevent_get_debug_info(phongo_compat_
199199
array_init_size(&retval, 6);
200200

201201
ADD_ASSOC_STRING(&retval, "commandName", intern->command_name);
202-
ADD_ASSOC_INT64(&retval, "durationMicros", (int64_t) intern->duration_micros);
202+
ADD_ASSOC_INT64(&retval, "durationMicros", intern->duration_micros);
203203

204-
sprintf(operation_id, "%" PRIu64, intern->operation_id);
204+
snprintf(operation_id, sizeof(operation_id), "%" PRId64, intern->operation_id);
205205
ADD_ASSOC_STRING(&retval, "operationId", operation_id);
206206

207207
if (!php_phongo_bson_to_zval_ex(intern->reply, &reply_state)) {
@@ -211,7 +211,7 @@ static HashTable* php_phongo_commandsucceededevent_get_debug_info(phongo_compat_
211211

212212
ADD_ASSOC_ZVAL(&retval, "reply", &reply_state.zchild);
213213

214-
sprintf(request_id, "%" PRIu64, intern->request_id);
214+
snprintf(request_id, sizeof(request_id), "%" PRId64, intern->request_id);
215215
ADD_ASSOC_STRING(&retval, "requestId", request_id);
216216

217217
{

src/phongo_structs.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ typedef struct {
261261
zval manager;
262262
char* command_name;
263263
uint32_t server_id;
264-
uint64_t operation_id;
265-
uint64_t request_id;
266-
uint64_t duration_micros;
264+
int64_t operation_id;
265+
int64_t request_id;
266+
int64_t duration_micros;
267267
bson_t* reply;
268268
zval z_error;
269269
bool has_service_id;
@@ -276,8 +276,8 @@ typedef struct {
276276
zval manager;
277277
char* command_name;
278278
uint32_t server_id;
279-
uint64_t operation_id;
280-
uint64_t request_id;
279+
int64_t operation_id;
280+
int64_t request_id;
281281
bson_t* command;
282282
char* database_name;
283283
bool has_service_id;
@@ -290,9 +290,9 @@ typedef struct {
290290
zval manager;
291291
char* command_name;
292292
uint32_t server_id;
293-
uint64_t operation_id;
294-
uint64_t request_id;
295-
uint64_t duration_micros;
293+
int64_t operation_id;
294+
int64_t request_id;
295+
int64_t duration_micros;
296296
bson_t* reply;
297297
bool has_service_id;
298298
bson_oid_t service_id;

0 commit comments

Comments
 (0)