This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: py_blake2*_new_impl produces inconsistent error messages, and raises OverflowError where ValueError might be better
Type: behavior Stage:
Components: Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Oren Milman, christian.heimes, serhiy.storchaka, xiang.zhang
Priority: normal Keywords:

Created on 2017-03-17 12:08 by Oren Milman, last changed 2022-04-11 14:58 by admin.

Messages (2)
msg289757 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-03-17 12:07
1. currently, py_blake2s_new_impl and py_blake2b_new_impl might produce
   inconsistent error messages (note that the first one happens on platforms
   where sizeof(long) > 4):
   >>> hashlib.blake2b(leaf_size=1 << 32)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   OverflowError: leaf_size is too large
   >>> hashlib.blake2b(leaf_size=1 << 1000)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   OverflowError: Python int too large to convert to C unsigned long
   
   >>> hashlib.blake2b(depth=256)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   ValueError: depth must be between 1 and 255
   >>> hashlib.blake2b(depth=256 << 1000)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   OverflowError: Python int too large to convert to C long
   
   there are similar inconsistent error messages when the function receives big
   and small out of range values for other arguments, too.
   
2. it might be better to raise a ValueError in the following cases:
   >>> hashlib.blake2b(leaf_size=-1)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   OverflowError: can't convert negative value to unsigned int

   >>> hashlib.blake2b(depth=-1 << 1000)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   OverflowError: Python int too large to convert to C long

   and maybe also in this case?
   >>> hashlib.blake2b(depth=1 << 1000)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   OverflowError: Python int too large to convert to C long

   this might be considered as a sub-issue of #29833.


note that solving the issue for leaf_size might be easier after #29834 is
resolved, as one could add something like the following to the if block of
(leaf_size == (unsigned long) -1 && PyErr_Occurred()):

               err = PyErr_Occurred();
               if (PyErr_GivenExceptionMatches(err, PyExc_OverflowError) ||
                   PyErr_GivenExceptionMatches(err, PyExc_ValueError)) {
                   PyErr_SetString(err,
                                   "leaf_size must be between 0 and 2**32-1");
               }

however, depth and other arguments are parsed by py_blake*_new, which is
generated by the argument clinic, so ISTM that solving the issue for them might
be harder.


(this issue was created while working on #15988, as can be seen in the code
review comments of PR 668.)
msg289759 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-03-17 13:06
I don't catch up with the progress but just FYI, the documentation of OverflowError states this situation: https://docs.python.org/3/library/exceptions.html#OverflowError. (I wanted to raise similar issues before ;-)
History
Date User Action Args
2022-04-11 14:58:44adminsetgithub: 74021
2017-03-17 13:17:24christian.heimessetnosy: + christian.heimes
2017-03-17 13:06:59xiang.zhangsetnosy: + xiang.zhang
messages: + msg289759
2017-03-17 12:08:00Oren Milmancreate