diff --git a/ext/gd/gd.c b/ext/gd/gd.c index 35edc9403cdbe..7646924e41653 100644 --- a/ext/gd/gd.c +++ b/ext/gd/gd.c @@ -798,8 +798,8 @@ PHP_FUNCTION(imagesetstyle) num_styles = zend_hash_num_elements(Z_ARRVAL_P(styles)); if (num_styles == 0) { - php_error_docref(NULL, E_WARNING, "styles array must not be empty"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "styles array must not be empty"); + return; } /* copy the style values in the stylearr */ @@ -828,9 +828,14 @@ PHP_FUNCTION(imagecreatetruecolor) return; } - if (x_size <= 0 || y_size <= 0 || x_size >= INT_MAX || y_size >= INT_MAX) { - php_error_docref(NULL, E_WARNING, "Invalid image dimensions"); - RETURN_FALSE; + if (x_size <= 0 || x_size >= INT_MAX) { + php_error_parameter_validation(NULL, 0, "Invalid width"); + return; + } + + if (y_size <= 0 || y_size >= INT_MAX) { + php_error_parameter_validation(NULL, 1, "Invalid height"); + return; } im = gdImageCreateTrueColor(x_size, y_size); @@ -880,9 +885,10 @@ PHP_FUNCTION(imagetruecolortopalette) } if (ncolors <= 0 || ZEND_LONG_INT_OVFL(ncolors)) { - php_error_docref(NULL, E_WARNING, "Number of colors has to be greater than zero and no more than %d", INT_MAX); - RETURN_FALSE; + php_error_parameter_validation(NULL, 2, "Number of colors has to be greater than zero and no more than %d", INT_MAX); + return; } + if (gdImageTrueColorToPalette(im, dither, (int)ncolors)) { RETURN_TRUE; } else { @@ -937,21 +943,20 @@ PHP_FUNCTION(imagecolormatch) result = gdImageColorMatch(im1, im2); switch (result) { case -1: - php_error_docref(NULL, E_WARNING, "Image1 must be TrueColor" ); - RETURN_FALSE; - break; + php_error_parameter_validation(NULL, 0, "Image1 must be TrueColor"); + return; + case -2: - php_error_docref(NULL, E_WARNING, "Image2 must be Palette" ); - RETURN_FALSE; - break; + php_error_parameter_validation(NULL, 1, "Image2 must be Palette"); + return; + case -3: - php_error_docref(NULL, E_WARNING, "Image1 and Image2 must be the same size" ); - RETURN_FALSE; - break; + php_error_parameter_validation(NULL, 1, "Image1 and Image2 must be the same size"); + return; + case -4: - php_error_docref(NULL, E_WARNING, "Image2 must have at least one color" ); - RETURN_FALSE; - break; + php_error_parameter_validation(NULL, 1, "Image2 must have at least one color"); + return; } RETURN_TRUE; @@ -1101,10 +1106,10 @@ PHP_FUNCTION(imagelayereffect) } /* }}} */ -#define CHECK_RGBA_RANGE(component, name) \ +#define CHECK_RGBA_RANGE(component, name, parameter_index) \ if (component < 0 || component > gd##name##Max) { \ - php_error_docref(NULL, E_WARNING, #name " component is out of range"); \ - RETURN_FALSE; \ + php_error_parameter_validation(NULL, parameter_index, #name " is out of range"); \ + return; \ } /* {{{ proto int imagecolorallocatealpha(resource im, int red, int green, int blue, int alpha) @@ -1124,10 +1129,10 @@ PHP_FUNCTION(imagecolorallocatealpha) return; } - CHECK_RGBA_RANGE(red, Red); - CHECK_RGBA_RANGE(green, Green); - CHECK_RGBA_RANGE(blue, Blue); - CHECK_RGBA_RANGE(alpha, Alpha); + CHECK_RGBA_RANGE(red, Red, 1); + CHECK_RGBA_RANGE(green, Green, 2); + CHECK_RGBA_RANGE(blue, Blue, 3); + CHECK_RGBA_RANGE(alpha, Alpha, 4); ct = gdImageColorAllocateAlpha(im, red, green, blue, alpha); if (ct < 0) { @@ -1153,10 +1158,10 @@ PHP_FUNCTION(imagecolorresolvealpha) return; } - CHECK_RGBA_RANGE(red, Red); - CHECK_RGBA_RANGE(green, Green); - CHECK_RGBA_RANGE(blue, Blue); - CHECK_RGBA_RANGE(alpha, Alpha); + CHECK_RGBA_RANGE(red, Red, 1); + CHECK_RGBA_RANGE(green, Green, 2); + CHECK_RGBA_RANGE(blue, Blue, 3); + CHECK_RGBA_RANGE(alpha, Alpha, 4); RETURN_LONG(gdImageColorResolveAlpha(im, red, green, blue, alpha)); } @@ -1178,10 +1183,10 @@ PHP_FUNCTION(imagecolorclosestalpha) return; } - CHECK_RGBA_RANGE(red, Red); - CHECK_RGBA_RANGE(green, Green); - CHECK_RGBA_RANGE(blue, Blue); - CHECK_RGBA_RANGE(alpha, Alpha); + CHECK_RGBA_RANGE(red, Red, 1); + CHECK_RGBA_RANGE(green, Green, 2); + CHECK_RGBA_RANGE(blue, Blue, 3); + CHECK_RGBA_RANGE(alpha, Alpha, 4); RETURN_LONG(gdImageColorClosestAlpha(im, red, green, blue, alpha)); } @@ -1203,10 +1208,10 @@ PHP_FUNCTION(imagecolorexactalpha) return; } - CHECK_RGBA_RANGE(red, Red); - CHECK_RGBA_RANGE(green, Green); - CHECK_RGBA_RANGE(blue, Blue); - CHECK_RGBA_RANGE(alpha, Alpha); + CHECK_RGBA_RANGE(red, Red, 1); + CHECK_RGBA_RANGE(green, Green, 2); + CHECK_RGBA_RANGE(blue, Blue, 3); + CHECK_RGBA_RANGE(alpha, Alpha, 4); RETURN_LONG(gdImageColorExactAlpha(im, red, green, blue, alpha)); } @@ -1271,8 +1276,8 @@ PHP_FUNCTION(imagegrabwindow) window = (HWND) lwindow_handle; if (!IsWindow(window)) { - php_error_docref(NULL, E_NOTICE, "Invalid window handle"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Invalid window handle"); + return; } hdc = GetDC(0); @@ -1466,9 +1471,13 @@ PHP_FUNCTION(imagecreate) return; } - if (x_size <= 0 || y_size <= 0 || x_size >= INT_MAX || y_size >= INT_MAX) { - php_error_docref(NULL, E_WARNING, "Invalid image dimensions"); - RETURN_FALSE; + if (x_size <= 0 || x_size >= INT_MAX) { + php_error_parameter_validation(NULL, 0, "Invalid image dimensions for width"); + } + + if (y_size <= 0 || y_size >= INT_MAX) { + php_error_parameter_validation(NULL, 1, "Invalid image dimensions for height"); + return; } im = gdImageCreate(x_size, y_size); @@ -1615,8 +1624,8 @@ PHP_FUNCTION(imagecreatefromstring) } if (ZSTR_LEN(data) < sizeof(sig)) { - php_error_docref(NULL, E_WARNING, "Empty string or invalid image"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Empty string or invalid image"); + return; } memcpy(sig, ZSTR_VAL(data), sizeof(sig)); @@ -1628,8 +1637,8 @@ PHP_FUNCTION(imagecreatefromstring) #ifdef HAVE_GD_JPG im = _php_image_create_from_string(data, "JPEG", gdImageCreateFromJpegCtx); #else - php_error_docref(NULL, E_WARNING, "No JPEG support in this PHP build"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "No JPEG support in this PHP build"); + return; #endif break; @@ -1637,8 +1646,8 @@ PHP_FUNCTION(imagecreatefromstring) #ifdef HAVE_GD_PNG im = _php_image_create_from_string(data, "PNG", gdImageCreateFromPngCtx); #else - php_error_docref(NULL, E_WARNING, "No PNG support in this PHP build"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "No PNG support in this PHP build"); + return; #endif break; @@ -1663,18 +1672,18 @@ PHP_FUNCTION(imagecreatefromstring) im = _php_image_create_from_string(data, "WEBP", gdImageCreateFromWebpCtx); break; #else - php_error_docref(NULL, E_WARNING, "No WEBP support in this PHP build"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "No WEBP support in this PHP build"); + return; #endif default: - php_error_docref(NULL, E_WARNING, "Data is not in a recognized format"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Data is not in a recognized format"); + return; } if (!im) { - php_error_docref(NULL, E_WARNING, "Couldn't create GD Image Stream out of Data"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Couldn't create GD Image Stream out of Data"); + return; } RETURN_RES(zend_register_resource(im, le_gd)); @@ -1699,10 +1708,18 @@ static void _php_image_create_from(INTERNAL_FUNCTION_PARAMETERS, int image_type, if (zend_parse_parameters(ZEND_NUM_ARGS(), "pllll", &file, &file_len, &srcx, &srcy, &width, &height) == FAILURE) { return; } - if (width < 1 || height < 1) { - php_error_docref(NULL, E_WARNING, "Zero width or height not allowed"); - RETURN_FALSE; + + if (width < 1) { + php_error_parameter_validation(NULL, 3, "Zero width not allowed"); + return; } + + if (height < 1) { + php_error_parameter_validation(NULL, 4, "Zero height not allowed"); + return; + } + + } else { if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &file, &file_len) == FAILURE) { return; @@ -2144,9 +2161,9 @@ PHP_FUNCTION(imagecolorallocate) return; } - CHECK_RGBA_RANGE(red, Red); - CHECK_RGBA_RANGE(green, Green); - CHECK_RGBA_RANGE(blue, Blue); + CHECK_RGBA_RANGE(red, Red, 1); + CHECK_RGBA_RANGE(green, Green, 2); + CHECK_RGBA_RANGE(blue, Blue, 3); ct = gdImageColorAllocate(im, red, green, blue); if (ct < 0) { @@ -2201,15 +2218,15 @@ PHP_FUNCTION(imagecolorat) if (im->tpixels && gdImageBoundsSafe(im, x, y)) { RETURN_LONG(gdImageTrueColorPixel(im, x, y)); } else { - php_error_docref(NULL, E_NOTICE, "" ZEND_LONG_FMT "," ZEND_LONG_FMT " is out of bounds", x, y); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "" ZEND_LONG_FMT "," ZEND_LONG_FMT " is out of bounds", x, y); + return; } } else { if (im->pixels && gdImageBoundsSafe(im, x, y)) { RETURN_LONG(im->pixels[y][x]); } else { - php_error_docref(NULL, E_NOTICE, "" ZEND_LONG_FMT "," ZEND_LONG_FMT " is out of bounds", x, y); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "" ZEND_LONG_FMT "," ZEND_LONG_FMT " is out of bounds", x, y); + return; } } } @@ -2231,9 +2248,9 @@ PHP_FUNCTION(imagecolorclosest) return; } - CHECK_RGBA_RANGE(red, Red); - CHECK_RGBA_RANGE(green, Green); - CHECK_RGBA_RANGE(blue, Blue); + CHECK_RGBA_RANGE(red, Red, 1); + CHECK_RGBA_RANGE(green, Green, 2); + CHECK_RGBA_RANGE(blue, Blue, 3); RETURN_LONG(gdImageColorClosest(im, red, green, blue)); } @@ -2255,9 +2272,9 @@ PHP_FUNCTION(imagecolorclosesthwb) return; } - CHECK_RGBA_RANGE(red, Red); - CHECK_RGBA_RANGE(green, Green); - CHECK_RGBA_RANGE(blue, Blue); + CHECK_RGBA_RANGE(red, Red, 1); + CHECK_RGBA_RANGE(green, Green, 2); + CHECK_RGBA_RANGE(blue, Blue, 3); RETURN_LONG(gdImageColorClosestHWB(im, red, green, blue)); } @@ -2291,8 +2308,8 @@ PHP_FUNCTION(imagecolordeallocate) gdImageColorDeallocate(im, col); RETURN_TRUE; } else { - php_error_docref(NULL, E_WARNING, "Color index %d out of range", col); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Color index %d out of range", col); + return; } } /* }}} */ @@ -2313,9 +2330,9 @@ PHP_FUNCTION(imagecolorresolve) return; } - CHECK_RGBA_RANGE(red, Red); - CHECK_RGBA_RANGE(green, Green); - CHECK_RGBA_RANGE(blue, Blue); + CHECK_RGBA_RANGE(red, Red, 1); + CHECK_RGBA_RANGE(green, Green, 2); + CHECK_RGBA_RANGE(blue, Blue, 3); RETURN_LONG(gdImageColorResolve(im, red, green, blue)); } @@ -2337,9 +2354,9 @@ PHP_FUNCTION(imagecolorexact) return; } - CHECK_RGBA_RANGE(red, Red); - CHECK_RGBA_RANGE(green, Green); - CHECK_RGBA_RANGE(blue, Blue); + CHECK_RGBA_RANGE(red, Red, 1); + CHECK_RGBA_RANGE(green, Green, 2); + CHECK_RGBA_RANGE(blue, Blue, 3); RETURN_LONG(gdImageColorExact(im, red, green, blue)); } @@ -2362,10 +2379,10 @@ PHP_FUNCTION(imagecolorset) return; } - CHECK_RGBA_RANGE(red, Red); - CHECK_RGBA_RANGE(green, Green); - CHECK_RGBA_RANGE(blue, Blue); - CHECK_RGBA_RANGE(alpha, Alpha); + CHECK_RGBA_RANGE(red, Red, 2); + CHECK_RGBA_RANGE(green, Green, 3); + CHECK_RGBA_RANGE(blue, Blue, 4); + CHECK_RGBA_RANGE(alpha, Alpha, 5); col = color; @@ -2407,8 +2424,8 @@ PHP_FUNCTION(imagecolorsforindex) add_assoc_long(return_value,"blue", gdImageBlue(im,col)); add_assoc_long(return_value,"alpha", gdImageAlpha(im,col)); } else { - php_error_docref(NULL, E_WARNING, "Color index %d out of range", col); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Color index %d out of range", col); + return; } } /* }}} */ @@ -2426,9 +2443,14 @@ PHP_FUNCTION(imagegammacorrect) return; } - if ( input <= 0.0 || output <= 0.0 ) { - php_error_docref(NULL, E_WARNING, "Gamma values should be positive"); - RETURN_FALSE; + if ( input <= 0.0) { + php_error_parameter_validation(NULL, 1, "Gamma input should be positive"); + return; + } + + if (output <= 0.0) { + php_error_parameter_validation(NULL, 2, "Gamma output should be positive"); + return; } gamma = input / output; @@ -2768,16 +2790,16 @@ static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled) nelem = zend_hash_num_elements(Z_ARRVAL_P(POINTS)); if (nelem < 6) { - php_error_docref(NULL, E_WARNING, "You must have at least 3 points in your array"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "You must have at least 3 points in your array"); + return; } if (npoints <= 0) { - php_error_docref(NULL, E_WARNING, "You must give a positive number of points"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 2, "You must give a positive number of points"); + return; } if (nelem < npoints * 2) { - php_error_docref(NULL, E_WARNING, "Trying to use %d points in array with only %d points", npoints, nelem/2); - RETURN_FALSE; + php_error_parameter_validation(NULL, 2, "Trying to use %d points in array with only %d points", npoints, nelem/2); + return; } points = (gdPointPtr) safe_emalloc(npoints, sizeof(gdPoint), 0); @@ -3162,18 +3184,34 @@ PHP_FUNCTION(imagecopyresized) srcX = SX; srcY = SY; - srcH = SH; srcW = SW; + srcH = SH; dstX = DX; dstY = DY; - dstH = DH; dstW = DW; + dstH = DH; - if (dstW <= 0 || dstH <= 0 || srcW <= 0 || srcH <= 0) { - php_error_docref(NULL, E_WARNING, "Invalid image dimensions"); - RETURN_FALSE; + if (dstW <= 0) { + php_error_parameter_validation(NULL, 6, "Destination width must be positive"); + return; + } + + if (dstH <= 0) { + php_error_parameter_validation(NULL, 7, "Destination height must be positive"); + return; + } + + if (srcW <= 0) { + php_error_parameter_validation(NULL, 8, "Source width must be positive"); + return; + } + + if (srcH <= 0) { + php_error_parameter_validation(NULL, 9, "Source height must be positive"); + return; } + gdImageCopyResized(im_dst, im_src, dstX, dstY, srcX, srcY, dstW, dstH, srcW, srcH); RETURN_TRUE; } @@ -3678,23 +3716,23 @@ PHP_FUNCTION(imageconvolution) nelem = zend_hash_num_elements(Z_ARRVAL_P(hash_matrix)); if (nelem != 3) { - php_error_docref(NULL, E_WARNING, "You must have 3x3 array"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "You must have 3x3 array"); + return; } for (i=0; i<3; i++) { if ((var = zend_hash_index_find(Z_ARRVAL_P(hash_matrix), (i))) != NULL && Z_TYPE_P(var) == IS_ARRAY) { if (zend_hash_num_elements(Z_ARRVAL_P(var)) != 3 ) { - php_error_docref(NULL, E_WARNING, "You must have 3x3 array"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "You must have 3x3 array"); + return; } for (j=0; j<3; j++) { if ((var2 = zend_hash_index_find(Z_ARRVAL_P(var), j)) != NULL) { matrix[i][j] = (float) zval_get_double(var2); } else { - php_error_docref(NULL, E_WARNING, "You must have a 3x3 matrix"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "You must have a 3x3 matrix"); + return; } } } @@ -3740,8 +3778,8 @@ PHP_FUNCTION(imageflip) break; default: - php_error_docref(NULL, E_WARNING, "Unknown flip mode"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Unknown flip mode"); + return; } RETURN_TRUE; @@ -3794,29 +3832,29 @@ PHP_FUNCTION(imagecrop) if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") -1)) != NULL) { rect.x = zval_get_long(tmp); } else { - php_error_docref(NULL, E_WARNING, "Missing x position"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Missing x position"); + return; } if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "y", sizeof("y") - 1)) != NULL) { rect.y = zval_get_long(tmp); } else { - php_error_docref(NULL, E_WARNING, "Missing y position"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Missing y position"); + return; } if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "width", sizeof("width") - 1)) != NULL) { rect.width = zval_get_long(tmp); } else { - php_error_docref(NULL, E_WARNING, "Missing width"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Missing width"); + return; } if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "height", sizeof("height") - 1)) != NULL) { rect.height = zval_get_long(tmp); } else { - php_error_docref(NULL, E_WARNING, "Missing height"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Missing height"); + return; } im_crop = gdImageCrop(im, &rect); @@ -3859,15 +3897,16 @@ PHP_FUNCTION(imagecropauto) case GD_CROP_THRESHOLD: if (color < 0 || (!gdImageTrueColor(im) && color >= gdImageColorsTotal(im))) { - php_error_docref(NULL, E_WARNING, "Color argument missing with threshold mode"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 3, "Color argument missing with threshold mode"); + return; } im_crop = gdImageCropThreshold(im, color, (float) threshold); break; default: - php_error_docref(NULL, E_WARNING, "Unknown crop mode"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Unknown crop mode %d", (int)mode); + return; + } if (im_crop == NULL) { RETURN_FALSE; @@ -3912,8 +3951,14 @@ PHP_FUNCTION(imagescale) } } - if (tmp_h <= 0 || tmp_h > INT_MAX || tmp_w <= 0 || tmp_w > INT_MAX) { - RETURN_FALSE; + if (tmp_h <= 0 || tmp_h > INT_MAX) { + php_error_parameter_validation(NULL, 2, "Out of bounds"); + return; + } + + if (tmp_w <= 0 || tmp_w > INT_MAX) { + php_error_parameter_validation(NULL, 1, "Out of bounds"); + return; } new_width = tmp_w; @@ -3959,8 +4004,8 @@ PHP_FUNCTION(imageaffine) } if ((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_affine))) != 6) { - php_error_docref(NULL, E_WARNING, "Affine array must have six elements"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Affine array must have six elements"); + return; } for (i = 0; i < nelems; i++) { @@ -3976,7 +4021,7 @@ PHP_FUNCTION(imageaffine) affine[i] = zval_get_double(zval_affine_elem); break; default: - php_error_docref(NULL, E_WARNING, "Invalid type for element %i", i); + php_error_parameter_validation(NULL, 1, "Invalid type for element %i", i); RETURN_FALSE; } } @@ -3986,29 +4031,29 @@ PHP_FUNCTION(imageaffine) if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") - 1)) != NULL) { rect.x = zval_get_long(tmp); } else { - php_error_docref(NULL, E_WARNING, "Missing x position"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 2, "Missing x position"); + return; } if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "y", sizeof("y") - 1)) != NULL) { rect.y = zval_get_long(tmp); } else { - php_error_docref(NULL, E_WARNING, "Missing y position"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 2, "Missing y position"); + return; } if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "width", sizeof("width") - 1)) != NULL) { rect.width = zval_get_long(tmp); } else { - php_error_docref(NULL, E_WARNING, "Missing width"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 2, "Missing width"); + return; } if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "height", sizeof("height") - 1)) != NULL) { rect.height = zval_get_long(tmp); } else { - php_error_docref(NULL, E_WARNING, "Missing height"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 2, "Missing height"); + return; } pRect = ▭ } else { @@ -4050,21 +4095,22 @@ PHP_FUNCTION(imageaffinematrixget) case GD_AFFINE_SCALE: { double x, y; if (!options || Z_TYPE_P(options) != IS_ARRAY) { - php_error_docref(NULL, E_WARNING, "Array expected as options"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Array expected as options"); + return; } + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(options), "x", sizeof("x") - 1)) != NULL) { x = zval_get_double(tmp); } else { - php_error_docref(NULL, E_WARNING, "Missing x position"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Missing x position"); + return; } if ((tmp = zend_hash_str_find(Z_ARRVAL_P(options), "y", sizeof("y") - 1)) != NULL) { y = zval_get_double(tmp); } else { - php_error_docref(NULL, E_WARNING, "Missing y position"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Missing y position"); + return; } if (type == GD_AFFINE_TRANSLATE) { @@ -4072,6 +4118,7 @@ PHP_FUNCTION(imageaffinematrixget) } else { res = gdAffineScale(affine, x, y); } + break; } @@ -4081,8 +4128,8 @@ PHP_FUNCTION(imageaffinematrixget) double angle; if (!options) { - php_error_docref(NULL, E_WARNING, "Number is expected as option"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Number is expected as option"); + return; } angle = zval_get_double(options); @@ -4098,8 +4145,8 @@ PHP_FUNCTION(imageaffinematrixget) } default: - php_error_docref(NULL, E_WARNING, "Invalid type for element " ZEND_LONG_FMT, type); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Invalid type for element " ZEND_LONG_FMT, type); + return; } if (res == GD_FALSE) { @@ -4129,9 +4176,14 @@ PHP_FUNCTION(imageaffinematrixconcat) return; } - if (((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_m1))) != 6) || (nelems = zend_hash_num_elements(Z_ARRVAL_P(z_m2))) != 6) { - php_error_docref(NULL, E_WARNING, "Affine arrays must have six elements"); - RETURN_FALSE; + if ((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_m1))) != 6) { + php_error_parameter_validation(NULL, 0, "Affine array must have six elements"); + return; + } + + if ((nelems = zend_hash_num_elements(Z_ARRVAL_P(z_m2))) != 6) { + php_error_parameter_validation(NULL, 1, "Affine arrays must have six elements"); + return; } for (i = 0; i < 6; i++) { @@ -4147,8 +4199,8 @@ PHP_FUNCTION(imageaffinematrixconcat) m1[i] = zval_get_double(tmp); break; default: - php_error_docref(NULL, E_WARNING, "Invalid type for element %i", i); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Invalid type for element %i", i); + return; } } if ((tmp = zend_hash_index_find(Z_ARRVAL_P(z_m2), i)) != NULL) { @@ -4163,8 +4215,8 @@ PHP_FUNCTION(imageaffinematrixconcat) m2[i] = zval_get_double(tmp); break; default: - php_error_docref(NULL, E_WARNING, "Invalid type for element %i", i); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Invalid type for element %i", i); + return; } } } diff --git a/ext/hash/hash.c b/ext/hash/hash.c index 2fbff0d102299..248c78ca7b280 100644 --- a/ext/hash/hash.c +++ b/ext/hash/hash.c @@ -129,13 +129,13 @@ static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_ ops = php_hash_fetch_ops(algo, algo_len); if (!ops) { - php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Unknown hashing algorithm: %s", algo); + return; } if (isfilename) { if (CHECK_NULL_PATH(data, data_len)) { - php_error_docref(NULL, E_WARNING, "Invalid path"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Invalid path"); + return; } stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context)); if (!stream) { @@ -252,18 +252,18 @@ static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename, ops = php_hash_fetch_ops(algo, algo_len); if (!ops) { - php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Unknown hashing algorithm: %s", algo); + return; } else if (!ops->is_crypto) { - php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", algo); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Non-cryptographic hashing algorithm: %s", algo); + return; } if (isfilename) { if (CHECK_NULL_PATH(data, data_len)) { - php_error_docref(NULL, E_WARNING, "Invalid path"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Invalid path"); + return; } stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context)); if (!stream) { @@ -358,19 +358,19 @@ PHP_FUNCTION(hash_init) ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo)); if (!ops) { - php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo)); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Unknown hashing algorithm: %s", ZSTR_VAL(algo)); + return; } if (options & PHP_HASH_HMAC) { if (!ops->is_crypto) { - php_error_docref(NULL, E_WARNING, "HMAC requested with a non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo)); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "HMAC requested with a non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo)); + return; } if (!key || (ZSTR_LEN(key) == 0)) { /* Note: a zero length key is no key at all */ - php_error_docref(NULL, E_WARNING, "HMAC requested without a key"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 2, "HMAC requested without a key"); + return; } } @@ -637,28 +637,28 @@ PHP_FUNCTION(hash_hkdf) ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo)); if (!ops) { - php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo)); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Unknown hashing algorithm: %s", ZSTR_VAL(algo)); + return; } if (!ops->is_crypto) { - php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo)); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo)); + return; } if (ZSTR_LEN(ikm) == 0) { - php_error_docref(NULL, E_WARNING, "Input keying material cannot be empty"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Input keying material cannot be empty"); + return; } if (length < 0) { - php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length); - RETURN_FALSE; + php_error_parameter_validation(NULL, 2, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length); + return; } else if (length == 0) { length = ops->digest_size; } else if (length > (zend_long) (ops->digest_size * 255)) { - php_error_docref(NULL, E_WARNING, "Length must be less than or equal to %zd: " ZEND_LONG_FMT, ops->digest_size * 255, length); - RETURN_FALSE; + php_error_parameter_validation(NULL, 2, "Length must be less than or equal to %zd: " ZEND_LONG_FMT, ops->digest_size * 255, length); + return; } context = emalloc(ops->context_size); @@ -737,27 +737,27 @@ PHP_FUNCTION(hash_pbkdf2) ops = php_hash_fetch_ops(algo, algo_len); if (!ops) { - php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Unknown hashing algorithm: %s", algo); + return; } else if (!ops->is_crypto) { - php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", algo); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Non-cryptographic hashing algorithm: %s", algo); + return; } if (iterations <= 0) { - php_error_docref(NULL, E_WARNING, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations); - RETURN_FALSE; + php_error_parameter_validation(NULL, 3, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations); + return; } if (length < 0) { - php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length); - RETURN_FALSE; + php_error_parameter_validation(NULL, 4, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length); + return; } if (salt_len > INT_MAX - 4) { - php_error_docref(NULL, E_WARNING, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len); - RETURN_FALSE; + php_error_parameter_validation(NULL, 2, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len); + return; } context = emalloc(ops->context_size); @@ -862,13 +862,13 @@ PHP_FUNCTION(hash_equals) /* We only allow comparing string to prevent unexpected results. */ if (Z_TYPE_P(known_zval) != IS_STRING) { - php_error_docref(NULL, E_WARNING, "Expected known_string to be a string, %s given", zend_zval_type_name(known_zval)); - RETURN_FALSE; + php_error_parameter_validation(NULL, 0, "Expected known_string to be a string, %s given", zend_zval_type_name(known_zval)); + return; } if (Z_TYPE_P(user_zval) != IS_STRING) { - php_error_docref(NULL, E_WARNING, "Expected user_string to be a string, %s given", zend_zval_type_name(user_zval)); - RETURN_FALSE; + php_error_parameter_validation(NULL, 1, "Expected user_string to be a string, %s given", zend_zval_type_name(user_zval)); + return; } if (Z_STRLEN_P(known_zval) != Z_STRLEN_P(user_zval)) { @@ -1052,8 +1052,8 @@ PHP_FUNCTION(mhash_keygen_s2k) bytes = (int)l_bytes; if (bytes <= 0){ - php_error_docref(NULL, E_WARNING, "the byte parameter must be greater than 0"); - RETURN_FALSE; + php_error_parameter_validation(NULL, 3, "The byte parameter must be greater than 0"); + return; } salt_len = MIN(salt_len, SALT_SIZE); diff --git a/ext/hash/tests/exceptional.inc b/ext/hash/tests/exceptional.inc new file mode 100644 index 0000000000000..b57f85303a374 --- /dev/null +++ b/ext/hash/tests/exceptional.inc @@ -0,0 +1,12 @@ +getMessage() . "\n"; + } + } diff --git a/ext/hash/tests/hash_equals.phpt b/ext/hash/tests/hash_equals.phpt index 0c8ab42f93b81..29f5c54723d43 100644 --- a/ext/hash/tests/hash_equals.phpt +++ b/ext/hash/tests/hash_equals.phpt @@ -2,21 +2,25 @@ Hash: hash_equals() test --FILE-- hash_equals("same", "same")); + trycatch_dump(fn() => hash_equals("not1same", "not2same")); + trycatch_dump(fn() => hash_equals("short", "longer")); + trycatch_dump(fn() => hash_equals("longer", "short")); + trycatch_dump(fn() => hash_equals("", "notempty")); + trycatch_dump(fn() => hash_equals("notempty", "")); + trycatch_dump(fn() => hash_equals("", "")); + trycatch_dump(fn() => hash_equals(123, "NaN")); + trycatch_dump(fn() => hash_equals("NaN", 123)); + trycatch_dump(fn() => hash_equals(123, 123)); + trycatch_dump(fn() => hash_equals(null, "")); + trycatch_dump(fn() => hash_equals(null, 123)); + trycatch_dump(fn() => hash_equals(null, null)); + ?> ---EXPECTF-- +--EXPECT-- bool(true) bool(false) bool(false) @@ -24,21 +28,9 @@ bool(false) bool(false) bool(false) bool(true) - -Warning: hash_equals(): Expected known_string to be a string, int given in %s on line %d -bool(false) - -Warning: hash_equals(): Expected user_string to be a string, int given in %s on line %d -bool(false) - -Warning: hash_equals(): Expected known_string to be a string, int given in %s on line %d -bool(false) - -Warning: hash_equals(): Expected known_string to be a string, null given in %s on line %d -bool(false) - -Warning: hash_equals(): Expected known_string to be a string, null given in %s on line %d -bool(false) - -Warning: hash_equals(): Expected known_string to be a string, null given in %s on line %d -bool(false) +[ErrorException] Parameter 1 is invalid: Expected known_string to be a string, int given +[ErrorException] Parameter 2 is invalid: Expected user_string to be a string, int given +[ErrorException] Parameter 1 is invalid: Expected known_string to be a string, int given +[ErrorException] Parameter 1 is invalid: Expected known_string to be a string, null given +[ErrorException] Parameter 1 is invalid: Expected known_string to be a string, null given +[ErrorException] Parameter 1 is invalid: Expected known_string to be a string, null given diff --git a/ext/hash/tests/hash_error.phpt b/ext/hash/tests/hash_error.phpt index f879d491d79a8..76ad7dd2fa869 100644 --- a/ext/hash/tests/hash_error.phpt +++ b/ext/hash/tests/hash_error.phpt @@ -3,6 +3,8 @@ Hash: hash() function : error conditions --FILE-- hash('foo', '')); ?> ===Done=== --EXPECTF-- -*** Testing hash() : error conditions *** - --- Testing hash() function with invalid hash algorithm -- +Warning: require_once(/mnt/c/users/mark/source/repos/local-php-src/src/ext/hash/tests/exceptional.php): failed to open stream: No such file or directory in %s on line %d -Warning: hash(): Unknown hashing algorithm: foo in %s on line %d -bool(false) -===Done=== +Fatal error: require_once(): Failed opening required '/mnt/c/users/mark/source/repos/local-php-src/src/ext/hash/tests/exceptional.php' (include_path='.:') in %s on line %d diff --git a/ext/hash/tests/hash_file_error.phpt b/ext/hash/tests/hash_file_error.phpt index b001001861aad..d41710588b501 100644 --- a/ext/hash/tests/hash_file_error.phpt +++ b/ext/hash/tests/hash_file_error.phpt @@ -4,6 +4,9 @@ Hash: hash_file() function : error conditions Felix De Vliegher --FILE-- hash_file( 'foobar', $filename ) ); echo "\n-- Testing hash_file() function with a non-existent file --\n"; -var_dump( hash_file( 'md5', 'nonexistent.txt' ) ); +trycatch_dump(fn() => hash_file( 'md5', 'nonexistent.txt' ) ); ?> ===DONE=== @@ -37,12 +40,10 @@ unlink( $filename ); *** Testing hash_file() : error conditions *** -- Testing hash_file() function with an unknown algorithm -- - -Warning: hash_file(): Unknown hashing algorithm: %s in %s on line %d -bool(false) +[ErrorException] Parameter 1 is invalid: Unknown hashing algorithm: foobar -- Testing hash_file() function with a non-existent file -- -Warning: hash_file(%s): failed to open stream: No such file or directory in %s on line %d +Warning: hash_file(nonexistent.txt): failed to open stream: No such file or directory in %s on line %d bool(false) ===DONE=== diff --git a/ext/hash/tests/hash_hkdf_edges.phpt b/ext/hash/tests/hash_hkdf_edges.phpt index 82acdbab04ffb..6c5111e822b47 100644 --- a/ext/hash/tests/hash_hkdf_edges.phpt +++ b/ext/hash/tests/hash_hkdf_edges.phpt @@ -3,6 +3,8 @@ Hash: hash_hkdf() function: edge cases --FILE-- hash_hkdf('jOaAt', $ikm)); ?> --EXPECTF-- @@ -25,6 +28,4 @@ Length < digestSize: 98b16391063ece Length % digestSize != 0: 98b16391063ecee006a3ca8ee5776b1e5f Algo name case-sensitivity: true Non-crypto algo name case-sensitivity: - -Warning: hash_hkdf(): Non-cryptographic hashing algorithm: jOaAt in %s on line %d -bool(false) +[ErrorException] Parameter 1 is invalid: Non-cryptographic hashing algorithm: jOaAt diff --git a/ext/hash/tests/hash_hkdf_error.phpt b/ext/hash/tests/hash_hkdf_error.phpt index e5903f126a48f..8367d5cde2c73 100644 --- a/ext/hash/tests/hash_hkdf_error.phpt +++ b/ext/hash/tests/hash_hkdf_error.phpt @@ -3,6 +3,9 @@ Hash: hash_hkdf() function: error conditions --FILE-- hash_hkdf('foo', $ikm)); echo "\n-- Testing hash_hkdf() function with non-cryptographic hash algorithm --\n"; -var_dump(hash_hkdf('adler32', $ikm)); -var_dump(hash_hkdf('crc32', $ikm)); -var_dump(hash_hkdf('crc32b', $ikm)); -var_dump(hash_hkdf('fnv132', $ikm)); -var_dump(hash_hkdf('fnv1a32', $ikm)); -var_dump(hash_hkdf('fnv164', $ikm)); -var_dump(hash_hkdf('fnv1a64', $ikm)); -var_dump(hash_hkdf('joaat', $ikm)); +trycatch_dump(fn() => hash_hkdf('adler32', $ikm)); +trycatch_dump(fn() => hash_hkdf('crc32', $ikm)); +trycatch_dump(fn() => hash_hkdf('crc32b', $ikm)); +trycatch_dump(fn() => hash_hkdf('fnv132', $ikm)); +trycatch_dump(fn() => hash_hkdf('fnv1a32', $ikm)); +trycatch_dump(fn() => hash_hkdf('fnv164', $ikm)); +trycatch_dump(fn() => hash_hkdf('fnv1a64', $ikm)); +trycatch_dump(fn() => hash_hkdf('joaat', $ikm)); echo "\n-- Testing hash_hkdf() function with invalid parameters --\n"; -var_dump(hash_hkdf('sha1', '')); -var_dump(hash_hkdf('sha1', $ikm, -1)); -var_dump(hash_hkdf('sha1', $ikm, 20 * 255 + 1)); // Length can't be more than 255 times the hash digest size +trycatch_dump(fn() => hash_hkdf('sha1', '')); +trycatch_dump(fn() => hash_hkdf('sha1', $ikm, -1)); +trycatch_dump(fn() => hash_hkdf('sha1', $ikm, 20 * 255 + 1)); // Length can't be more than 255 times the hash digest size + ?> ===Done=== ---EXPECTF-- +--EXPECT-- *** Testing hash_hkdf(): error conditions *** -- Testing hash_hkdf() function with invalid hash algorithm -- - -Warning: hash_hkdf(): Unknown hashing algorithm: foo in %s on line %d -bool(false) +[ErrorException] Parameter 1 is invalid: Unknown hashing algorithm: foo -- Testing hash_hkdf() function with non-cryptographic hash algorithm -- - -Warning: hash_hkdf(): Non-cryptographic hashing algorithm: adler32 in %s on line %d -bool(false) - -Warning: hash_hkdf(): Non-cryptographic hashing algorithm: crc32 in %s on line %d -bool(false) - -Warning: hash_hkdf(): Non-cryptographic hashing algorithm: crc32b in %s on line %d -bool(false) - -Warning: hash_hkdf(): Non-cryptographic hashing algorithm: fnv132 in %s on line %d -bool(false) - -Warning: hash_hkdf(): Non-cryptographic hashing algorithm: fnv1a32 in %s on line %d -bool(false) - -Warning: hash_hkdf(): Non-cryptographic hashing algorithm: fnv164 in %s on line %d -bool(false) - -Warning: hash_hkdf(): Non-cryptographic hashing algorithm: fnv1a64 in %s on line %d -bool(false) - -Warning: hash_hkdf(): Non-cryptographic hashing algorithm: joaat in %s on line %d -bool(false) +[ErrorException] Parameter 1 is invalid: Non-cryptographic hashing algorithm: adler32 +[ErrorException] Parameter 1 is invalid: Non-cryptographic hashing algorithm: crc32 +[ErrorException] Parameter 1 is invalid: Non-cryptographic hashing algorithm: crc32b +[ErrorException] Parameter 1 is invalid: Non-cryptographic hashing algorithm: fnv132 +[ErrorException] Parameter 1 is invalid: Non-cryptographic hashing algorithm: fnv1a32 +[ErrorException] Parameter 1 is invalid: Non-cryptographic hashing algorithm: fnv164 +[ErrorException] Parameter 1 is invalid: Non-cryptographic hashing algorithm: fnv1a64 +[ErrorException] Parameter 1 is invalid: Non-cryptographic hashing algorithm: joaat -- Testing hash_hkdf() function with invalid parameters -- - -Warning: hash_hkdf(): Input keying material cannot be empty in %s on line %d -bool(false) - -Warning: hash_hkdf(): Length must be greater than or equal to 0: -1 in %s on line %d -bool(false) - -Warning: hash_hkdf(): Length must be less than or equal to 5100: 5101 in %s on line %d -bool(false) +[ErrorException] Parameter 2 is invalid: Input keying material cannot be empty +[ErrorException] Parameter 3 is invalid: Length must be greater than or equal to 0: -1 +[ErrorException] Parameter 3 is invalid: Length must be less than or equal to 5100: 5101 ===Done=== diff --git a/ext/hash/tests/hash_hmac_error.phpt b/ext/hash/tests/hash_hmac_error.phpt index 1ed3e54127416..a1c1b43618021 100644 --- a/ext/hash/tests/hash_hmac_error.phpt +++ b/ext/hash/tests/hash_hmac_error.phpt @@ -2,6 +2,9 @@ Hash: hash_hmac() function : basic functionality --FILE-- hash_hmac('foo', $data, $key)); echo "\n-- Testing hash_hmac() function with non-cryptographic hash algorithm --\n"; -var_dump(hash_hmac('crc32', $data, $key)); +trycatch_dump(fn() => hash_hmac('crc32', $data, $key)); ?> ===Done=== --EXPECTF-- -*** Testing hash_hmac() : error conditions *** - --- Testing hash_hmac() function with invalid hash algorithm -- - -Warning: hash_hmac(): Unknown hashing algorithm: foo in %s on line %d -bool(false) - --- Testing hash_hmac() function with non-cryptographic hash algorithm -- - -Warning: hash_hmac(): Non-cryptographic hashing algorithm: crc32 in %s on line %d -bool(false) -===Done=== +Parse error: syntax error, unexpected ''/exceptional.inc'' (T_CONSTANT_ENCAPSED_STRING) in %s on line %d diff --git a/ext/hash/tests/hash_hmac_file_error.phpt b/ext/hash/tests/hash_hmac_file_error.phpt index 67a4a8550f1d4..f8f619d28269d 100644 --- a/ext/hash/tests/hash_hmac_file_error.phpt +++ b/ext/hash/tests/hash_hmac_file_error.phpt @@ -3,6 +3,8 @@ Hash: hash_hmac_file() function : basic functionality --FILE-- hash_hmac_file('foo', $file, $key, TRUE)); echo "\n-- Testing hash_hmac_file() function with non-cryptographic hash algorithm --\n"; -hash_hmac_file('crc32', $file, $key, TRUE); +trycatch_dump(fn() => hash_hmac_file('crc32', $file, $key, TRUE)); echo "\n-- Testing hash_hmac_file() function with bad path --\n"; -hash_hmac_file('md5', $file.chr(0).$file, $key, TRUE); +trycatch_dump(fn() => hash_hmac_file('md5', $file.chr(0).$file, $key, TRUE)); ?> ===Done=== ---EXPECTF-- +--EXPECT-- *** Testing hash() : error conditions *** -- Testing hash_hmac_file() function with invalid hash algorithm -- - -Warning: hash_hmac_file(): Unknown hashing algorithm: foo in %s on line %d +[ErrorException] Parameter 1 is invalid: Unknown hashing algorithm: foo -- Testing hash_hmac_file() function with non-cryptographic hash algorithm -- - -Warning: hash_hmac_file(): Non-cryptographic hashing algorithm: crc32 in %s on line %d +[ErrorException] Parameter 1 is invalid: Non-cryptographic hashing algorithm: crc32 -- Testing hash_hmac_file() function with bad path -- - -Warning: hash_hmac_file(): Invalid path in %s on line %d +[ErrorException] Parameter 2 is invalid: Invalid path ===Done=== diff --git a/ext/hash/tests/hash_init_error.phpt b/ext/hash/tests/hash_init_error.phpt index 9105b96801ad1..4a69ba6a5df72 100644 --- a/ext/hash/tests/hash_init_error.phpt +++ b/ext/hash/tests/hash_init_error.phpt @@ -2,32 +2,28 @@ Hash: hash_init() function - errors test --FILE-- hash_init('dummy')); echo "-- Testing hash_init() function with HASH_HMAC and non-cryptographic algorithms --\n"; -var_dump(hash_init('crc32', HASH_HMAC)); +trycatch_dump(fn() => hash_init('crc32', HASH_HMAC)); echo "-- Testing hash_init() function with HASH_HMAC and no key --\n"; -var_dump(hash_init('md5', HASH_HMAC)); -var_dump(hash_init('md5', HASH_HMAC, null)); +trycatch_dump(fn() => hash_init('md5', HASH_HMAC)); +trycatch_dump(fn() => hash_init('md5', HASH_HMAC, null)); + ?> ---EXPECTF-- +--EXPECT-- *** Testing hash_init(): error conditions *** -- Testing hash_init() function with unknown algorithms -- - -Warning: hash_init(): Unknown hashing algorithm: dummy in %s on line %d -bool(false) +[ErrorException] Parameter 1 is invalid: Unknown hashing algorithm: dummy -- Testing hash_init() function with HASH_HMAC and non-cryptographic algorithms -- - -Warning: hash_init(): HMAC requested with a non-cryptographic hashing algorithm: crc32 in %s on line %d -bool(false) +[ErrorException] Parameter 1 is invalid: HMAC requested with a non-cryptographic hashing algorithm: crc32 -- Testing hash_init() function with HASH_HMAC and no key -- - -Warning: hash_init(): HMAC requested without a key %s on line %d -bool(false) - -Warning: hash_init(): HMAC requested without a key %s on line %d -bool(false) +[ErrorException] Parameter 3 is invalid: HMAC requested without a key +[ErrorException] Parameter 3 is invalid: HMAC requested without a key diff --git a/ext/hash/tests/hash_pbkdf2_error.phpt b/ext/hash/tests/hash_pbkdf2_error.phpt index a7fd08649bb43..b760cc4db13e3 100644 --- a/ext/hash/tests/hash_pbkdf2_error.phpt +++ b/ext/hash/tests/hash_pbkdf2_error.phpt @@ -3,6 +3,8 @@ Hash: Test hash_pbkdf2() function : error functionality --FILE-- hash_pbkdf2('foo', $password, $salt, 1)); echo "\n-- Testing hash_pbkdf2() function with non-cryptographic hash algorithm --\n"; -var_dump(hash_pbkdf2('crc32', $password, $salt, 1)); +trycatch_dump(fn() => hash_pbkdf2('crc32', $password, $salt, 1)); echo "\n-- Testing hash_pbkdf2() function with invalid iterations --\n"; -var_dump(hash_pbkdf2('md5', $password, $salt, 0)); -var_dump(hash_pbkdf2('md5', $password, $salt, -1)); +trycatch_dump(fn() => hash_pbkdf2('md5', $password, $salt, 0)); +trycatch_dump(fn() => hash_pbkdf2('md5', $password, $salt, -1)); echo "\n-- Testing hash_pbkdf2() function with invalid length --\n"; -var_dump(hash_pbkdf2('md5', $password, $salt, 1, -1)); +trycatch_dump(fn() => hash_pbkdf2('md5', $password, $salt, 1, -1)); ?> ===Done=== ---EXPECTF-- +--EXPECT-- *** Testing hash_pbkdf2() : error conditions *** -- Testing hash_pbkdf2() function with invalid hash algorithm -- - -Warning: hash_pbkdf2(): Unknown hashing algorithm: foo in %s on line %d -bool(false) +[ErrorException] Parameter 1 is invalid: Unknown hashing algorithm: foo -- Testing hash_pbkdf2() function with non-cryptographic hash algorithm -- - -Warning: hash_pbkdf2(): Non-cryptographic hashing algorithm: crc32 in %s on line %d -bool(false) +[ErrorException] Parameter 1 is invalid: Non-cryptographic hashing algorithm: crc32 -- Testing hash_pbkdf2() function with invalid iterations -- - -Warning: hash_pbkdf2(): Iterations must be a positive integer: 0 in %s on line %d -bool(false) - -Warning: hash_pbkdf2(): Iterations must be a positive integer: -1 in %s on line %d -bool(false) +[ErrorException] Parameter 4 is invalid: Iterations must be a positive integer: 0 +[ErrorException] Parameter 4 is invalid: Iterations must be a positive integer: -1 -- Testing hash_pbkdf2() function with invalid length -- - -Warning: hash_pbkdf2(): Length must be greater than or equal to 0: -1 in %s on line %d -bool(false) +[ErrorException] Parameter 5 is invalid: Length must be greater than or equal to 0: -1 ===Done=== diff --git a/main/main.c b/main/main.c index 37894b4764314..c474814ec296d 100644 --- a/main/main.c +++ b/main/main.c @@ -1170,6 +1170,21 @@ PHPAPI ZEND_COLD void php_error_docref2(const char *docref, const char *param1, } /* }}} */ +PHPAPI ZEND_COLD void php_error_parameter_validation(const char* docref, int argno, const char* format, ...) { + char* complete_msg; + char* message; + va_list args; + + va_start(args, format); + zend_vspprintf(&message, 0, format, args); + va_end(args); + + spprintf(&complete_msg, 0, "Parameter %d is invalid: %s", argno + 1, (const char*)message); + zend_throw_exception(zend_ce_error_exception, complete_msg, E_ERROR); + efree(message); + efree(complete_msg); +} + #ifdef PHP_WIN32 PHPAPI ZEND_COLD void php_win32_docref2_from_error(DWORD error, const char *param1, const char *param2) { char *buf = php_win32_error_to_msg(error); diff --git a/main/php.h b/main/php.h index c74f76f2ab943..51465fb5c1991 100644 --- a/main/php.h +++ b/main/php.h @@ -341,10 +341,17 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ /* PHPAPI void php_error(int type, const char *format, ...); */ PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format, ...) PHP_ATTRIBUTE_FORMAT(printf, 3, 4); + PHPAPI ZEND_COLD void php_error_docref1(const char *docref, const char *param1, int type, const char *format, ...) PHP_ATTRIBUTE_FORMAT(printf, 4, 5); + PHPAPI ZEND_COLD void php_error_docref2(const char *docref, const char *param1, const char *param2, int type, const char *format, ...) PHP_ATTRIBUTE_FORMAT(printf, 5, 6); + +PHPAPI ZEND_COLD void php_error_parameter_validation(const char* docref, int argno, const char* format, ...) + PHP_ATTRIBUTE_FORMAT(printf, 3, 4); + + #ifdef PHP_WIN32 PHPAPI ZEND_COLD void php_win32_docref2_from_error(DWORD error, const char *param1, const char *param2); #endif