Php intval () decimal Security Vulnerability Analysis

Source: Internet
Author: User

This article first introduces the simple usage of php intval, which can convert characters, numbers, and decimals into gray-numeric data. However, some problems may occur during conversion, such as converting decimal 1.1 to 1, see the instance below.

Convert a variable to an integer.

Syntax: int intval (mixed var, int [base]);

Return Value: integer

Function types: PHP system functions

 
Description


This function converts a variable to an integer. The base parameter that can be omitted is the basis for conversion. The default value is 10. The converted variable var can be any type variable outside the array or class.


Security vulnerability analysis due to improper use of intval ()


The intval function has a feature: "conversion starts only when a number or positive or negative sign is encountered, and the conversion ends when a non-number or string ends ", in some applications, due to insufficient understanding of the intval function, improper use may cause security vulnerabilities caused by bypassing some security judgments.

Ii. Analysis

The Code is as follows: Copy code

PHP_FUNCTION (intval)

{

Zval ** num, ** arg_base;

Int base;


Switch (ZEND_NUM_ARGS ()){

Case 1:

If (zend_get_parameters_ex (1, & num) = FAILURE ){

WRONG_PARAM_COUNT;

}

Base = 10;

Break;

Case 2:

If (zend_get_parameters_ex (2, & num, & arg_base) = FAILURE ){

WRONG_PARAM_COUNT;

}

Convert_to_long_ex (arg_base );

Base = Z_LVAL_PP (arg_base );

Break;

Default:

WRONG_PARAM_COUNT;

}

RETVAL_ZVAL (* num, 1, 0 );

Convert_to_long_base (return_value, base );

}

Zend/zend_operators.c-> convert_to_long_base ()

......

Case IS_STRING:

Strval = Z_STRVAL_P (op );

Z_LVAL_P (op) = strtol (strval, NULL, base );

STR_FREE (strval );

Break;

When the intval function receives a string parameter, it calls convert_to_long_base () for processing, and then calls Z_LVAL_P (op) = strtol (strval, NULL, base). The strtol function is used to process the parameter.

The function prototype is as follows:

Long int strtol (const char * nptr, char ** endptr, int base );

This function converts the nptr parameter to a growth integer number based on the base parameter. The base parameter ranges from 2 to 36, or 0. the base parameter indicates the base mode. For example, if the base value is 10, the base value is 10. If the base value is 16, the base value is hexadecimal.

The process is as follows:

Strtol () scans the nptr parameter string, skips the leading space character, and ends the conversion only when a number or positive or negative sign is encountered, and return the result.

When intval is used in the if and so on, it will cause the judgment to be de-meaningful, leading to security vulnerabilities.

Iii. Test code

The Code is as follows: Copy code

<? Php
USD o = 0.1;
For ($ a = 1; $ a <100; $ a ++ ){
$ O + = 0.1;
Echo "<br/> intval ($ o) =". intval ($ o );
If (intval ($ o )){
Print ("& nbsp; true ");
} Else {
Print ("& nbsp; false ");
}
}?>

Output result:

Intval (0.2) = 0 false
Intval (0.3) = 0 false
Intval (0.4) = 0 false
Intval (0.5) = 0 false
Intval (0.6) = 0 false
Intval (0.7) = 0 false
Intval (0.8) = 0 false
Intval (0.9) = 0 false
Intval (1) = 0 false
Intval (1.1) = 1 true
Intval (1.2) = 1 true
Intval (1.3) = 1 true
Intval (1.4) = 1 true
Intval (1.5) = 1 true
Intval (1.6) = 1 true
Intval (1.7) = 1 true
Intval (1.8) = 1 true
Intval (1.9) = 1 true
Intval (2) = 2 true
.....

Differences between php intval and int

The Code is as follows: Copy code

$ T = '000000 ';

Echo gettype (int) ($ t ));
Echo 'Echo gettype (intval ($ t ));

// Output result: integer value: 165


$ T = 'a165 ';

Echo gettype (int) ($ t ));
Echo 'Echo gettype (intval ($ t ));

// Result integer value: 0, 0

$ String = "2a ";
$ String1 = intval ($ string );
Echo '$ string1 value:'. $ string1. '$ string2 value:'; // The variable is not output in single quotes and is output as is.
$ String2 = (int) ($ string );
Echo $ string2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.