Skip to content

Conversation

@wfelipew
Copy link

@wfelipew wfelipew commented Feb 4, 2015

Regex to check float values changed to accept "infinity" values and ignore case.
Quotes are added to prevent syntax error on PostgreSQL.

Regex to check float values changed to accept "infinity" values
and ignore case. Quotes are added to prevent syntax error on
PostgreSQL.
@laruence
Copy link
Member

laruence commented Feb 4, 2015

A test script (phpt) included is better

@wfelipew
Copy link
Author

wfelipew commented Feb 4, 2015

@laruence to test this patch i need connection with database, table and data. There is a way to mock this on test script (phpt) ?

@laruence
Copy link
Member

laruence commented Feb 5, 2015

@wfelipew please check out other test scripts under ext/pgsql/tests

@smalyshev
Copy link
Contributor

Also, this does not look like security issue, so it's probably not getting into 5.4.

@smalyshev smalyshev added the Bug label Feb 5, 2015
@laruence
Copy link
Member

laruence commented Feb 5, 2015

@smalyshev right, only for 5.5+

@wfelipew
Copy link
Author

wfelipew commented Feb 5, 2015

@laruence Test script included

@wfelipew
Copy link
Author

wfelipew commented Feb 5, 2015

@laruence @smalyshev
If this will fixed only 5.5, should i have to open another pull request against 5.5 branch ?

@laruence
Copy link
Member

laruence commented Feb 5, 2015

@wfelipew I will look tomorrow.. probably no .. we can cherry-pick it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this line for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is for single quote values, because if you send "infinity" values without quotes PostgreSQL throws it syntax error.
Since PostgreSQL accept single quotes for all kind of values, i put this without check if is a "inifinity" or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then this will slow down all pathes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, i will change it to only add quotes on "infinity" values.

Add quotes only if "infinity" values are received.
@wfelipew
Copy link
Author

wfelipew commented Feb 7, 2015

@laruence look if now is good

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you still introduce a new convert_match for all paths..

why not do:

if (php_pgsql_convert_match() == FAILURE){
      if (php_pgsql_convert_match("infinite") == SUCCESS) {
        add_quotes
     } else {
        err = 1;
     }
} else {
   //keep fast path un-touched
}

is that clear?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, its clear.. but now doing some tests i see that using this approachs (check with php_pgsql_convert_match to only add quotes on 'infinity' values) generates notices when the regex not match.

subs = (regmatch_t *)ecalloc(sizeof(regmatch_t), re.re_nsub+1);

    regerr = regexec(&re, str, re.re_nsub+1, subs, 0);
    if (regerr == REG_NOMATCH) {
#ifdef PHP_DEBUG
        php_error_docref(NULL TSRMLS_CC, E_NOTICE, "'%s' does not match with '%s'", str, regex);
#endif
        ret = FAILURE;
    }

Test result:

Notice: pg_update(): 'inf' does not match with '^([+-]{0,1}[0-9]+)|([+-]{0,1}[0-9]*[\.][0-9]+)|([+-]{0,1}[0-9]+[\.][0-9]*)$' in /opt/php/test_pgsql.php on line 19
string(52) "UPDATE "test_68638" SET "value"=E'inf' WHERE "id"=1;"

Notice: pg_update(): 'inf' does not match with '^([+-]{0,1}[0-9]+)|([+-]{0,1}[0-9]*[\.][0-9]+)|([+-]{0,1}[0-9]+[\.][0-9]*)$' in /opt/php/test_pgsql.php on line 21

Notice: pg_update(): '-inf' does not match with '^([+-]{0,1}[0-9]+)|([+-]{0,1}[0-9]*[\.][0-9]+)|([+-]{0,1}[0-9]+[\.][0-9]*)$' in /opt/php/test_pgsql.php on line 22

Notice: pg_update(): '+inf' does not match with '^([+-]{0,1}[0-9]+)|([+-]{0,1}[0-9]*[\.][0-9]+)|([+-]{0,1}[0-9]+[\.][0-9]*)$' in /opt/php/test_pgsql.php on line 23

I think one possible solutions to avoid this notice is to fork the "php_pgsql_convert_match" to "php_pgsql_match" that not generate this notice and use this function on the first "if" to not generate notice.

Like this:

if (php_pgsql_match() == FAILURE){
      if (php_pgsql_convert_match("infinite") == SUCCESS) {
        add_quotes
     } else {
        err = 1;
     }
} else {
   //keep fast path un-touched
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other solution is that:

else {
                            /* FIXME: better regex must be used */
                            if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^([+-]{0,1}[0-9]+)|([+-]{0,1}[0-9]*[\\.][0-9]+)|([+-]{0,1}[0-9]+[\\.][0-9]*)|([+-]{0,1}(inf)(inity){0,1})$", 1 TSRMLS_CC) == FAILURE) {
                                    err = 1;
                            }
                            else {
                                ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
                                if(strcasestr(Z_STRVAL_PP(val),"inf")!=0){
                                    php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
                                }
                            }
                        }

Do you think this strcasestr in all paths can significantly slow execution ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, looks a little ugly, but if you don't have better fix.. then I think we have to do in this way?

Check if value is "infinity" with strcasestr() to add quotes
@wfelipew
Copy link
Author

wfelipew commented Feb 9, 2015

@laruence check the last commit.. i think this is the better solution

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent

@laruence
Copy link
Member

@wfelipew okey, I am going to merge it... thanks

@wfelipew
Copy link
Author

@laruence Indent fixed

@laruence
Copy link
Member

sorry for delay, maybe do final check in weekend. thanks

@laruence
Copy link
Member

merged 5.5+ , 5.4 is security fix only..

but strcasestr is _GNU_SOURCE only...

thus at last I still prefer use the first idea I talked before..I remove the E_NOTICE if match fails.

thanks

@weltling
Copy link
Contributor

weltling commented Jun 3, 2016

Close as it's applied already.

Thanks.

@weltling weltling closed this Jun 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants