Projet

Général

Profil

Bug #48092

psycopg2.DataError : valeur en dehors des limites pour le type integer

Ajouté par Thomas Noël il y a plus de 3 ans. Mis à jour il y a 10 mois.

Statut:
Fermé
Priorité:
Normal
Assigné à:
-
Version cible:
-
Début:
28 octobre 2020
Echéance:
% réalisé:

0%

Temps estimé:
Patch proposed:
Non
Planning:
Non

Description

Sur une URI du genre /categorie/slug-formulaire/207631675722, on termine en crash avec :

Exception:
  type = '<class 'psycopg2.DataError'>', value = 'ERREUR:  la valeur « 207631675722 » est en dehors des limites pour le type integer
LINE 5:                             WHERE id = '207631675722'
                                               ^
Stack trace (most recent call first):
  File "/usr/lib/python3/dist-packages/wcs/sql.py", line 1823, in get
  1821                                     ', '.join(fields),
  1822                                     cls._table_name)
> 1823         cur.execute(sql_statement, {'id': str(id)})
  1824         row = cur.fetchone()
  1825         if row is None:

Historique

#1

Mis à jour par Thomas Noël il y a plus de 3 ans

à noter qu'en local, avec un système plus récent, j'obtiens une exception différente

Exception:
  type = '<class 'psycopg2.errors.NumericValueOutOfRange'>', value = 'value "398798797123" is out of range for type integer
LINE 5:                             WHERE id = '398798797123'

et que isinstance(psycopg2.errors.NumericValueOutOfRange, psycopg2.DataError) renvoie False :/

#2

Mis à jour par Thomas Noël il y a plus de 3 ans

Sans doute quelque chose comme

diff --git a/wcs/sql.py b/wcs/sql.py
index 85b52e5b..faceabc6 100644
--- a/wcs/sql.py
+++ b/wcs/sql.py
@@ -1163,7 +1163,9 @@ class SqlMixin(object):
     def get(cls, id, ignore_errors=False, ignore_migration=False):
         if cls._numerical_id or id is None:
             try:
-                int(id)
+                integer = int(id)
+                if integer < 0 or integer >= 2**31:  # PostgreSQL integer limit (4 bytes)
+                    raise ValueError
             except (TypeError, ValueError):
                 if ignore_errors and id is None:
                     return None
@@ -1802,7 +1804,9 @@ class SqlDataMixin(SqlMixin):
     @guard_postgres
     def get(cls, id, ignore_errors=False, ignore_migration=False):
         try:
-            int(id)
+            integer = int(id)
+            if integer < 0 or integer >= 2**31:  # PostgreSQL integer limit (4 bytes)
+                raise ValueError
         except (TypeError, ValueError):
             if ignore_errors:
                 return None

mais curieusement je n'ai pas encore trouvé le/les tests où on vérifie ce type d'accès

#3

Mis à jour par Benjamin Dauvergne il y a plus de 3 ans

Thomas Noël a écrit :

et que isinstance(psycopg2.errors.NumericValueOutOfRange, psycopg2.DataError) renvoie False :/

Peut-être parce qu'il faut utiliser isubclass ?

In [2]: issubclass(psycopg2.errors.NumericValueOutOfRange, psycopg2.DataError)
Out[2]: True

#4

Mis à jour par Frédéric Péters il y a 10 mois

  • Statut changé de Nouveau à Fermé

Traité en #79000 et #73963.

Formats disponibles : Atom PDF