Fix and improve reflection

- use "p" for path (security check for nul byte)
- fix number of args for some methods
- add type hinting
- throw standard exception (simplify)
- fix test suite for PHP 7.0 to 8.0
This commit is contained in:
Remi Collet
2020-06-22 16:22:37 +02:00
parent d36a2de544
commit cf0ce5a01e
14 changed files with 48 additions and 50 deletions
+1 -5
View File
@@ -17,11 +17,7 @@ PHP_FUNCTION(dlib_chinese_whispers)
std::vector<sample_pair> edges; std::vector<sample_pair> edges;
std::vector<unsigned long> labels; std::vector<unsigned long> labels;
if(zend_parse_parameters(ZEND_NUM_ARGS(), "a", &edges_arg) == FAILURE){ if(zend_parse_parameters_throw(ZEND_NUM_ARGS(), "a", &edges_arg) == FAILURE){
zend_throw_exception_ex(
zend_ce_exception,
0,
"Unable to parse edges in dlib_chinese_whispers");
return; return;
} }
+1 -1
View File
@@ -6,7 +6,7 @@
#define PHP_DLIB_CHINESE_WHISPERS_H #define PHP_DLIB_CHINESE_WHISPERS_H
ZEND_BEGIN_ARG_INFO_EX(dlib_chinese_whispers_arginfo, 0, 0, 1) ZEND_BEGIN_ARG_INFO_EX(dlib_chinese_whispers_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, edges) ZEND_ARG_ARRAY_INFO(0, edges, 0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
PHP_FUNCTION(dlib_chinese_whispers); PHP_FUNCTION(dlib_chinese_whispers);
+2 -4
View File
@@ -29,9 +29,8 @@ PHP_METHOD(CnnFaceDetection, __construct)
return; return;
} }
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p",
&sz_cnn_face_detection_model_path, &cnn_face_detection_model_path_len) == FAILURE){ &sz_cnn_face_detection_model_path, &cnn_face_detection_model_path_len) == FAILURE){
zend_throw_exception_ex(zend_ce_exception, 0, "Unable to parse face_detection_model_path");
return; return;
} }
@@ -53,8 +52,7 @@ PHP_METHOD(CnnFaceDetection, detect)
size_t img_path_len; size_t img_path_len;
long upsample_num = 0; long upsample_num = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &img_path, &img_path_len, &upsample_num) == FAILURE){ if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p|l", &img_path, &img_path_len, &upsample_num) == FAILURE){
zend_throw_exception_ex(zend_ce_exception, 0, "Unable to parse detect arguments");
RETURN_FALSE; RETURN_FALSE;
} }
+8 -4
View File
@@ -23,13 +23,17 @@ typedef struct _cnn_face_detection {
} cnn_face_detection; } cnn_face_detection;
ZEND_BEGIN_ARG_INFO_EX(cnn_face_detection_ctor_arginfo, 0, 0, 1) ZEND_BEGIN_ARG_INFO_EX(cnn_face_detection_ctor_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, cnn_face_detection_model_path) ZEND_ARG_TYPE_INFO(0, cnn_face_detection_model_path, IS_STRING, 0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
PHP_METHOD(CnnFaceDetection, __construct); PHP_METHOD(CnnFaceDetection, __construct);
ZEND_BEGIN_ARG_INFO_EX(cnn_face_detection_detect_arginfo, 0, 0, 2) ZEND_BEGIN_ARG_INFO_EX(cnn_face_detection_detect_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, img_path) ZEND_ARG_TYPE_INFO(0, img_path, IS_STRING, 0)
ZEND_ARG_INFO(0, upsample_num) #if PHP_VERSION_ID < 80000
ZEND_ARG_TYPE_INFO(0, upsample_num, IS_LONG, 0)
#else
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, upsample_num, IS_LONG, 0, "0")
#endif
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
PHP_METHOD(CnnFaceDetection, detect); PHP_METHOD(CnnFaceDetection, detect);
+1 -2
View File
@@ -17,8 +17,7 @@ PHP_FUNCTION(dlib_face_detection)
size_t img_path_len; size_t img_path_len;
long upsample_num = 0; long upsample_num = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &img_path, &img_path_len, &upsample_num) == FAILURE) { if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p|l", &img_path, &img_path_len, &upsample_num) == FAILURE) {
zend_throw_exception_ex(zend_ce_exception, 0, "Unable to parse dlib_face_detection arguments");
RETURN_FALSE; RETURN_FALSE;
} }
try { try {
+6 -2
View File
@@ -6,8 +6,12 @@
#define PHP_DLIB_FACE_DETECTION_H #define PHP_DLIB_FACE_DETECTION_H
ZEND_BEGIN_ARG_INFO_EX(dlib_face_detection_arginfo, 0, 0, 1) ZEND_BEGIN_ARG_INFO_EX(dlib_face_detection_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, img_path) ZEND_ARG_TYPE_INFO(0, img_path, IS_STRING, 0)
ZEND_ARG_INFO(0, upsample_num) #if PHP_VERSION_ID < 80000
ZEND_ARG_TYPE_INFO(0, upsample_num, IS_LONG, 0)
#else
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, upsample_num, IS_LONG, 0, "0")
#endif
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
PHP_FUNCTION(dlib_face_detection); PHP_FUNCTION(dlib_face_detection);
+3 -5
View File
@@ -27,7 +27,7 @@ PHP_FUNCTION(dlib_face_landmark_detection)
char *img_path; char *img_path;
size_t shape_predictor_file_path_len, img_path_len; size_t shape_predictor_file_path_len, img_path_len;
if(zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &shape_predictor_file_path, &shape_predictor_file_path_len, if(zend_parse_parameters_throw(ZEND_NUM_ARGS(), "ss", &shape_predictor_file_path, &shape_predictor_file_path_len,
&img_path, &img_path_len) == FAILURE){ &img_path, &img_path_len) == FAILURE){
RETURN_FALSE; RETURN_FALSE;
} }
@@ -83,9 +83,8 @@ PHP_METHOD(FaceLandmarkDetection, __construct)
} }
// Parse predictor model's path // Parse predictor model's path
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p",
&sz_shape_predictor_file_path, &shape_predictor_file_path_len) == FAILURE){ &sz_shape_predictor_file_path, &shape_predictor_file_path_len) == FAILURE){
zend_throw_exception_ex(zend_ce_exception, 0, "Unable to parse shape_predictor_file_path");
return; return;
} }
@@ -114,8 +113,7 @@ PHP_METHOD(FaceLandmarkDetection, detect)
// Parse path to image and bounding box. Bounding box is associative array of 4 elements - "top", "bottom", "left" and "right". // Parse path to image and bounding box. Bounding box is associative array of 4 elements - "top", "bottom", "left" and "right".
// //
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sa", &img_path, &img_path_len, &bounding_box) == FAILURE){ if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "pa", &img_path, &img_path_len, &bounding_box) == FAILURE){
zend_throw_exception_ex(zend_ce_exception, 0, "Unable to parse detect arguments");
return; return;
} }
+6 -6
View File
@@ -9,9 +9,9 @@
using namespace dlib; using namespace dlib;
ZEND_BEGIN_ARG_INFO_EX(dlib_face_landmark_detection_arginfo, 0, 0, 1) ZEND_BEGIN_ARG_INFO_EX(dlib_face_landmark_detection_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, shape_predictor_file_path) ZEND_ARG_TYPE_INFO(0, shape_predictor_file_path, IS_STRING, 0)
ZEND_ARG_INFO(0, img_path) ZEND_ARG_TYPE_INFO(0, img_path, IS_STRING, 0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
PHP_FUNCTION(dlib_face_landmark_detection); PHP_FUNCTION(dlib_face_landmark_detection);
@@ -21,13 +21,13 @@ typedef struct _face_landmark_detection {
} face_landmark_detection; } face_landmark_detection;
ZEND_BEGIN_ARG_INFO_EX(face_landmark_detection_ctor_arginfo, 0, 0, 1) ZEND_BEGIN_ARG_INFO_EX(face_landmark_detection_ctor_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, shape_predictor_file_path) ZEND_ARG_TYPE_INFO(0, shape_predictor_file_path, IS_STRING, 0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
PHP_METHOD(FaceLandmarkDetection, __construct); PHP_METHOD(FaceLandmarkDetection, __construct);
ZEND_BEGIN_ARG_INFO_EX(face_landmark_detection_detect_arginfo, 0, 0, 2) ZEND_BEGIN_ARG_INFO_EX(face_landmark_detection_detect_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, img_path) ZEND_ARG_TYPE_INFO(0, img_path, IS_STRING, 0)
ZEND_ARG_INFO(0, bounding_box) ZEND_ARG_ARRAY_INFO(0, bounding_box, 0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
PHP_METHOD(FaceLandmarkDetection, detect); PHP_METHOD(FaceLandmarkDetection, detect);
+2 -4
View File
@@ -26,9 +26,8 @@ PHP_METHOD(FaceRecognition, __construct)
return; return;
} }
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p",
&sz_face_recognition_model_path, &face_recognition_model_path_len) == FAILURE){ &sz_face_recognition_model_path, &face_recognition_model_path_len) == FAILURE){
zend_throw_exception_ex(zend_ce_exception, 0, "Unable to parse face_recognition_model_path");
return; return;
} }
@@ -73,8 +72,7 @@ PHP_METHOD(FaceRecognition, computeDescriptor)
zval *shape; zval *shape;
long num_jitters = 1; long num_jitters = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sa|l", &img_path, &img_path_len, &shape, &num_jitters) == FAILURE){ if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "pa|l", &img_path, &img_path_len, &shape, &num_jitters) == FAILURE){
zend_throw_exception_ex(zend_ce_exception, 0, "Unable to parse computeDescriptor arguments");
return; return;
} }
+9 -5
View File
@@ -44,14 +44,18 @@ typedef struct _face_recognition {
} face_recognition; } face_recognition;
ZEND_BEGIN_ARG_INFO_EX(face_recognition_ctor_arginfo, 0, 0, 1) ZEND_BEGIN_ARG_INFO_EX(face_recognition_ctor_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, face_recognition_model_path) ZEND_ARG_TYPE_INFO(0, face_recognition_model_path, IS_STRING, 0)
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
PHP_METHOD(FaceRecognition, __construct); PHP_METHOD(FaceRecognition, __construct);
ZEND_BEGIN_ARG_INFO_EX(face_recognition_compute_descriptor_arginfo, 0, 0, 3) ZEND_BEGIN_ARG_INFO_EX(face_recognition_compute_descriptor_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, img_path) ZEND_ARG_TYPE_INFO(0, img_path, IS_STRING, 0)
ZEND_ARG_INFO(0, landmarks) ZEND_ARG_ARRAY_INFO(0, landmarks, 0)
ZEND_ARG_INFO(0, num_jitters) #if PHP_VERSION_ID < 80000
ZEND_ARG_TYPE_INFO(0, num_jitters, IS_LONG, 0)
#else
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, num_jitters, IS_LONG, 0, "1")
#endif
ZEND_END_ARG_INFO() ZEND_END_ARG_INFO()
PHP_METHOD(FaceRecognition, computeDescriptor); PHP_METHOD(FaceRecognition, computeDescriptor);
@@ -6,10 +6,9 @@ Args given to chinese_whispers functions is not correct
<?php <?php
try { try {
dlib_chinese_whispers("foo"); dlib_chinese_whispers("foo");
} catch (Exception $e) { } catch (Error $e) {
var_dump($e->getMessage()); var_dump($e->getMessage());
} }
?> ?>
--EXPECTF-- --EXPECTF--
Warning: dlib_chinese_whispers() expects parameter 1 to be array, string given in %s on line 3 string(%d) "%s type array, string given"
string(46) "Unable to parse edges in dlib_chinese_whispers"
+3 -3
View File
@@ -6,10 +6,10 @@ Testing CnnFaceDetection constructor without arguments
<?php <?php
try { try {
new CnnFaceDetection(); new CnnFaceDetection();
} catch (Exception $e) { } catch (Error $e) {
var_dump($e->getMessage()); var_dump($e->getMessage());
} }
?> ?>
--EXPECTF-- --EXPECTF--
Warning: CnnFaceDetection::__construct() expects exactly 1 parameter, 0 given in %s on line 3 string(68) "CnnFaceDetection::__construct() expects exactly 1 parameter, 0 given"
string(41) "Unable to parse face_detection_model_path"
@@ -6,7 +6,7 @@ Testing FaceLandmarkDetection constructor without arguments
<?php <?php
try { try {
new FaceLandmarkDetection(); new FaceLandmarkDetection();
} catch (Exception $e) { } catch (Error $e) {
var_dump($e->getMessage()); var_dump($e->getMessage());
} }
try { try {
@@ -16,6 +16,5 @@ try {
} }
?> ?>
--EXPECTF-- --EXPECTF--
Warning: FaceLandmarkDetection::__construct() expects exactly 1 parameter, 0 given in %s on line 3 string(73) "FaceLandmarkDetection::__construct() expects exactly 1 parameter, 0 given"
string(41) "Unable to parse shape_predictor_file_path"
string(45) "Unable to open non-existent file for reading." string(45) "Unable to open non-existent file for reading."
+2 -3
View File
@@ -6,10 +6,9 @@ Testing FaceRecognition constructor without arguments
<?php <?php
try { try {
new FaceRecognition(); new FaceRecognition();
} catch (Exception $e) { } catch (Error $e) {
var_dump($e->getMessage()); var_dump($e->getMessage());
} }
?> ?>
--EXPECTF-- --EXPECTF--
Warning: FaceRecognition::__construct() expects exactly 1 parameter, 0 given in %s on line 3 string(67) "FaceRecognition::__construct() expects exactly 1 parameter, 0 given"
string(43) "Unable to parse face_recognition_model_path"