Skip to content

bignum didn't take care in where statment #783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
linbo opened this issue Apr 11, 2014 · 4 comments
Closed

bignum didn't take care in where statment #783

linbo opened this issue Apr 11, 2014 · 4 comments
Labels

Comments

@linbo
Copy link

linbo commented Apr 11, 2014

Here in my table, defined uid column type is bigint

| uid                     | bigint(20) unsigned | NO 

I query table filter by uid

var sql = "SELECT id from table_name where uid=?";
        connection.query(sql, [uid], function(err, results){
            connection.release();

But if I pass wrong uid, it also works.

if uid == 12345 works
then uid == 12345ff  works fine

Is it because bignum didn't do restrict conversion?

> bignum("123")
<BigNum 123>
> bignum("123f")
<BigNum 123>
@sidorares
Copy link
Member

yes, for bignum "123" and "123f" are same numbers (I guess you are using https://www.npmjs.org/package/bignum )

> a = bignum("123")
<BigNum 123>
> b = bignum("123f")
<BigNum 123>
> a == b
false
> a.eq(b)
true

why don't you just compare original strings for equality?

@dougwilson
Copy link
Member

@linbo we don't convert to bignum on the sending side; what is uid in your original example? Is it your own bignum instance, or a native JavaScript Number?

You'll notice that if you go to a MySQL console and run

SELECT id from table_name where uid='123f';

MySQL itself will select where uid is 123, so there is nothing this library can do.

mysql> CREATE TABLE test (`id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, `uid` BIGINT UNSIGNED);
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO test (`uid`) VALUES(123);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT id FROM test WHERE uid = 123;
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0.01 sec)

mysql> SELECT id FROM test WHERE uid = '123f';
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0.00 sec)

@dougwilson
Copy link
Member

If you need to, you'll need to manually do a typeof uid === 'number' check before you query the database.

@linbo
Copy link
Author

linbo commented Apr 12, 2014

@dougwilson thanks. uid is passed by HTTP GET request params. Looks I need check GET request params by myself before query MySQL.

dveeden pushed a commit to dveeden/mysql that referenced this issue Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants