@@ -1289,7 +1289,7 @@ static bool php_phongo_bson_state_parse_type(zval* options, const char* name, ph
1289
1289
return retval ;
1290
1290
} /* }}} */
1291
1291
1292
- static void field_path_map_element_set_info (php_phongo_field_path_map_element * element , php_phongo_bson_typemap_types type , zend_class_entry * ce TSRMLS_DC )
1292
+ static void field_path_map_element_set_info (php_phongo_field_path_map_element * element , php_phongo_bson_typemap_types type , zend_class_entry * ce )
1293
1293
{
1294
1294
element -> node_type = type ;
1295
1295
element -> node_ce = ce ;
@@ -1316,44 +1316,69 @@ static php_phongo_field_path_map_element* field_path_map_element_alloc(void)
1316
1316
return tmp ;
1317
1317
}
1318
1318
1319
+ static void field_path_map_element_dtor (php_phongo_field_path_map_element * element )
1320
+ {
1321
+ php_phongo_field_path_free (element -> entry );
1322
+ efree (element );
1323
+ }
1324
+
1319
1325
bool php_phongo_bson_state_add_field_path (php_phongo_bson_typemap * map , char * field_path_original , php_phongo_bson_typemap_types type , zend_class_entry * ce TSRMLS_DC )
1320
1326
{
1321
- char * saveptr ;
1322
- char * field_path_str = estrdup (field_path_original );
1323
- const char * element ;
1327
+ char * ptr = NULL ;
1328
+ char * segment_end = NULL ;
1324
1329
php_phongo_field_path_map_element * field_path_map_element ;
1325
1330
1331
+ if (field_path_original [0 ] == '.' ) {
1332
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "A 'fieldPaths' key may not start with a '.'" );
1333
+ return false;
1334
+ }
1335
+
1336
+ if (field_path_original [strlen (field_path_original ) - 1 ] == '.' ) {
1337
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "A 'fieldPaths' key may not end with a '.'" );
1338
+ return false;
1339
+ }
1340
+
1326
1341
field_path_map_element = field_path_map_element_alloc ();
1342
+ ptr = field_path_original ;
1327
1343
1328
1344
/* Loop over all the segments. A segment is delimited by a "." */
1329
- element = php_strtok_r (field_path_str , "." , & saveptr );
1330
- while (element ) {
1331
- php_phongo_field_path_push (field_path_map_element -> entry , element , PHONGO_FIELD_PATH_ITEM_NONE );
1332
- element = php_strtok_r (NULL , "." , & saveptr );
1345
+ while ((segment_end = strchr (ptr , '.' )) != NULL ) {
1346
+ char * tmp = NULL ;
1347
+
1348
+ /* Bail out if we have an empty segment */
1349
+ if (ptr == segment_end ) {
1350
+ field_path_map_element_dtor (field_path_map_element );
1351
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "A 'fieldPaths' key may not have an empty segment" );
1352
+ return false;
1353
+ }
1354
+
1355
+ tmp = calloc (1 , segment_end - ptr + 1 );
1356
+ memcpy (tmp , ptr , segment_end - ptr );
1357
+ php_phongo_field_path_push (field_path_map_element -> entry , tmp , PHONGO_FIELD_PATH_ITEM_NONE );
1358
+ free (tmp );
1359
+
1360
+ ptr = segment_end + 1 ;
1333
1361
}
1334
1362
1335
- efree (field_path_str );
1363
+ /* Add the last (or single) element */
1364
+ php_phongo_field_path_push (field_path_map_element -> entry , ptr , PHONGO_FIELD_PATH_ITEM_NONE );
1336
1365
1337
- field_path_map_element_set_info (field_path_map_element , type , ce TSRMLS_CC );
1366
+ field_path_map_element_set_info (field_path_map_element , type , ce );
1338
1367
map_add_field_path_element (map , field_path_map_element );
1339
1368
1340
1369
return true;
1341
1370
}
1342
1371
1343
- static void field_path_map_element_dtor (php_phongo_field_path_map_element * element )
1344
- {
1345
- php_phongo_field_path_free (element -> entry );
1346
- efree (element );
1347
- }
1348
-
1349
1372
void php_phongo_bson_typemap_dtor (php_phongo_bson_typemap * map )
1350
1373
{
1351
1374
size_t i ;
1352
1375
1353
- for (i = 0 ; i < map -> field_paths .size ; i ++ ) {
1354
- field_path_map_element_dtor (map -> field_paths .map [i ]);
1376
+ if (map -> field_paths .map ) {
1377
+ for (i = 0 ; i < map -> field_paths .size ; i ++ ) {
1378
+ field_path_map_element_dtor (map -> field_paths .map [i ]);
1379
+ }
1380
+ efree (map -> field_paths .map );
1355
1381
}
1356
- efree (map -> field_paths .map );
1357
1382
1358
1383
map -> field_paths .map = NULL ;
1359
1384
}
@@ -1394,6 +1419,11 @@ bool php_phongo_bson_state_parse_fieldpaths(zval* typemap, php_phongo_bson_typem
1394
1419
return false;
1395
1420
}
1396
1421
1422
+ if (strcmp (ZSTR_VAL (string_key ), "" ) == 0 ) {
1423
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "The 'fieldPaths' element may not be an empty string" );
1424
+ return false;
1425
+ }
1426
+
1397
1427
if (!php_phongo_bson_state_parse_type (fieldpaths , ZSTR_VAL (string_key ), & map_type , & map_ce TSRMLS_CC )) {
1398
1428
return false;
1399
1429
}
@@ -1425,6 +1455,11 @@ bool php_phongo_bson_state_parse_fieldpaths(zval* typemap, php_phongo_bson_typem
1425
1455
return false;
1426
1456
}
1427
1457
1458
+ if (strcmp (string_key , "" ) == 0 ) {
1459
+ phongo_throw_exception (PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC , "The 'fieldPaths' element may not be an empty string" );
1460
+ return false;
1461
+ }
1462
+
1428
1463
if (!php_phongo_bson_state_parse_type (fieldpaths , string_key , & map_type , & map_ce TSRMLS_CC )) {
1429
1464
return false;
1430
1465
}
0 commit comments