#10112: random_prime  does not handle erroneous input gracefully - it hangs
------------------------------------------+---------------------------------
   Reporter:  drkirkby                    |          Owner:  was                
        
       Type:  defect                      |         Status:  positive_review    
        
   Priority:  major                       |      Milestone:  sage-4.8           
        
  Component:  number theory               |       Keywords:                     
        
Work_issues:                              |       Upstream:  N/A                
        
   Reviewer:  David Kirkby, Johan Bosman  |         Author:  Mike Hansen, 
Francis Clarke
     Merged:                              |   Dependencies:                     
        
------------------------------------------+---------------------------------
Changes (by johanbosman):

  * status:  needs_review => positive_review
  * reviewer:  David Kirkby => David Kirkby, Johan Bosman


Old description:

> As noted at #10111 and
>
> http://groups.google.com/group/sage-
> devel/browse_thread/thread/6e8d6f28c915830d?hl=en
>
> {{{random_prime()}}} is not well documented.
>
> But {{{random_prime()}}} is also broken for some erroneous input.
> Programs should always handle poor input properly, which is the case of
> Sage probably means generating an error message.
>
> {{{
> sage: random_prime(0,True,-1) # Hangs
> sage: random_prime(0,False,-1) # Hangs
> sage: random_prime(1,0,-23) # Hangs
> sage: random_prime(-12,0,-12) # Hangs
> sage: random_prime(0, proof=False, lbound=-12) # Hangs
> }}}
>
> Having a negative lower bound can cause the function to hang, but this is
> not always the case, as the example below shows. Here an error message is
> generated.
>
> {{{
> sage: random_prime(-12,0,-4)
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call
> last)
>
> /export/home/drkirkby/sage-4.6.alpha3/<ipython console> in <module>()
>
> /export/home/drkirkby/sage-4.6.alpha3/local/lib/python2.6/site-
> packages/sage/rings/arith.pyc in random_prime(n, proof, lbound)
>    1144     n = ZZ(n)
>    1145     if n < lbound:
> -> 1146         raise ValueError, "n must be greater than lbound:
> %s"%(lbound)
>    1147     elif n == 2:
>    1148         return ZZ(n)
>
> ValueError: n must be greater than lbound: -4
> }}}
>
> Dave

New description:

 As noted at #10111 and

 http://groups.google.com/group/sage-
 devel/browse_thread/thread/6e8d6f28c915830d?hl=en

 {{{random_prime()}}} is not well documented.

 But {{{random_prime()}}} is also broken for some erroneous input. Programs
 should always handle poor input properly, which is the case of Sage
 probably means generating an error message.

 {{{
 sage: random_prime(0,True,-1) # Hangs
 sage: random_prime(0,False,-1) # Hangs
 sage: random_prime(1,0,-23) # Hangs
 sage: random_prime(-12,0,-12) # Hangs
 sage: random_prime(0, proof=False, lbound=-12) # Hangs
 }}}

 Having a negative lower bound can cause the function to hang, but this is
 not always the case, as the example below shows. Here an error message is
 generated.

 {{{
 sage: random_prime(-12,0,-4)
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)

 /export/home/drkirkby/sage-4.6.alpha3/<ipython console> in <module>()

 /export/home/drkirkby/sage-4.6.alpha3/local/lib/python2.6/site-
 packages/sage/rings/arith.pyc in random_prime(n, proof, lbound)
    1144     n = ZZ(n)
    1145     if n < lbound:
 -> 1146         raise ValueError, "n must be greater than lbound:
 %s"%(lbound)
    1147     elif n == 2:
    1148         return ZZ(n)

 ValueError: n must be greater than lbound: -4
 }}}

 Dave

 Apply
  1. [attachment: trac_10112.patch]
  1. [attachment: trac_10112-replacement-extra.patch]
 to the Sage library.

--

Comment:

 Replying to [comment:14 drkirkby]:
 > Is there any reason this should not be changed to  positive review if
 the slowdown is very small?
 >
 I don't think so.  The code looks okay, is well documented, and passes all
 tests.

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/10112#comment:15>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to