Negatives...

imageschool arithmetic course we all know that two negatives make a positive. Your humble servant all my life I was sure of this seemingly unshakable axiom. But recently, an event occurred that changed the world, and forced to watch the new look at familiar things.

In the process of developing administrative tools for club drunk needed to function anulowania all the results of specific games. It would seem that can be easier. Change the status of the game rolled back denormalization data with statistics of players, invalidiem operational caches that affect these data, and the trick is done. But bundles PostgreSQL and psycopg2 on this account was his own opinion that does not coincide with ideas of editorial.



Simplified the problematic query looked like this:

the cursor.execute("update pref_player set games=games-1, rating=rating-%s where player_id=%s", (rating, player_id))

That is, we have decremented the total number of games and anullirovat players scored in this game. The query is parameterized. Screened regular means psycopg2. There seems to be just a place to make a mistake. But really, when negative value rating, this query would turn into:

update pref_player set games=games-1, rating=rating

That is, there is an EXPLOSION, the BASE BREAKS IN the INTESTINE, and further in the text. In other words, a happily received two consecutive minus like a comment, and beat the data into the cabbage. And the driver psycopg2 all this joy missed without a cry. Though, probably, could determine that the request is the review and the number of screened parameters is not true.

This can be treated quite simply. Enough is rating=rating-%s say for example rating=rating-(%s) and everything is working as it should, But the fact of unconditional trust for processing parameterized queries of the database drivers need to be questioned.

Watch yourself, be careful and may the force be with you.
Article based on information from habrahabr.ru

Comments

Popular posts from this blog

Powershell and Cyrillic in the console (updated)

Active/Passive PostgreSQL Cluster, using Pacemaker, Corosync

Automatic deployment ElasticBeanstalk using Bitbucket Pipelines