Ticket #13875: trac_13875.patch

File trac_13875.patch, 4.8 KB (added by dcoudert, 8 years ago)
  • sage/graphs/distances_all_pairs.pyx

    # HG changeset patch
    # User dcoudert <david.coudert@inria.fr>
    # Date 1356694568 -3600
    # Node ID 59c3a848bdf197f4e443cf767423e5efa1b631f8
    # Parent  386b880c3c51919efc089b5d710ffee3bbeaea84
    trac #13875 -- Test memory allocation in distances_all_pairs
    
    diff --git a/sage/graphs/distances_all_pairs.pyx b/sage/graphs/distances_all_pairs.pyx
    a b  
    9191      and ``None``. These case happens when the input is a disconnected graph,
    9292      or a non-strongly-connected digraph.
    9393
     94    - A memory error is raised when data structures allocation failed. This
     95      could happen with large graphs on computers with low memory space.
     96
    9497    .. WARNING::
    9598
    9699        The function ``all_pairs_shortest_path_BFS`` has **no reason** to be
     
    159162    # The list of waiting vertices, the beginning and the end of the list
    160163
    161164    cdef unsigned short * waiting_list = <unsigned short *> sage_malloc(n*sizeof(short))
     165    if waiting_list==NULL:
     166        raise MemoryError()
    162167    cdef unsigned short waiting_beginning = 0
    163168    cdef unsigned short waiting_end = 0
    164169
    165170    cdef int * degree = <int *> sage_malloc(n*sizeof(int))
     171    if degree==NULL:
     172        sage_free(waiting_list)
     173        raise MemoryError()
    166174
    167175    cdef unsigned short source
    168176    cdef unsigned short v, u
     
    183191        c_distances = distances
    184192    else:
    185193        c_distances = <unsigned short *> sage_malloc( n * sizeof(unsigned short *))
    186 
     194        if c_distances==NULL:
     195            sage_free(waiting_list)
     196            sage_free(degree)
     197            raise MemoryError()
    187198    cdef int * outneighbors
    188199    cdef int o_n_size
    189200
     
    298309
    299310    cdef int n = G.order()
    300311    cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short *))
     312    if distances==NULL:
     313        raise MemoryError()
    301314    cdef unsigned short * predecessors = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short *))
     315    if predecessors==NULL:
     316        sage_free(distances)
     317        raise MemoryError()
    302318    all_pairs_shortest_path_BFS(G, predecessors, distances, NULL)
    303319
    304320    sage_free(distances)
     
    385401
    386402    cdef int n = G.order()
    387403    cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short *))
     404    if distances==NULL:
     405        raise MemoryError()
    388406    all_pairs_shortest_path_BFS(G, NULL, distances, NULL)
    389407
    390408    return distances
     
    502520        return {}, {}
    503521
    504522    cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short *))
     523    if distances==NULL:
     524        raise MemoryError()
    505525    cdef unsigned short * c_distances = distances
    506526    cdef unsigned short * predecessor = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short *))
     527    if predecessor==NULL:
     528        sage_free(distances)
     529        raise MemoryError()
    507530    cdef unsigned short * c_predecessor = predecessor
    508531
    509532    all_pairs_shortest_path_BFS(G, predecessor, distances, NULL)
     
    557580    cdef int n = G.order()
    558581
    559582    cdef unsigned short * ecc = <unsigned short *> sage_malloc(n*sizeof(unsigned short *))
     583    if ecc==NULL:
     584        raise MemoryError()
    560585    cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short *))
     586    if distances==NULL:
     587        sage_free(ecc)
     588        raise MemoryError()
    561589    all_pairs_shortest_path_BFS(G, NULL, distances, ecc)
    562590    sage_free(distances)
    563591
     
    876904
    877905    # init dist
    878906    t_dist = <unsigned short *> sage_malloc(n*n*sizeof(short))
     907    if t_dist==NULL:
     908        raise MemoryError()
    879909    dist = <unsigned short **> sage_malloc(n*sizeof(short *))
     910    if dist==NULL:
     911        sage_free(t_dist)
     912        raise MemoryError()
    880913    dist[0] = t_dist
    881914    for 1 <= i< n:
    882915        dist[i] = dist[i-1] + n
     
    890923    if paths:
    891924        # init prec
    892925        t_prec = <unsigned short *> sage_malloc(n*n*sizeof(short))
     926        if t_prec==NULL:
     927            sage_free(t_dist)
     928            sage_free(dist)
     929            raise MemoryError()
    893930        prec = <unsigned short **> sage_malloc(n*sizeof(short *))
     931        if prec==NULL:
     932            sage_free(t_dist)
     933            sage_free(dist)
     934            sage_free(t_prec)
     935            raise MemoryError()
    894936        prec[0] = t_prec
    895937        for 1 <= i< n:
    896938            prec[i] = prec[i-1] + n
  • sage/graphs/hyperbolicity.pyx

    diff --git a/sage/graphs/hyperbolicity.pyx b/sage/graphs/hyperbolicity.pyx
    a b  
    936936            # We compute the distances and store the results in a 2D array, and
    937937            # the diameter
    938938            _distances_ = c_distances_all_pairs(H)
    939             if _distances_ == NULL:
    940                 raise MemoryError
    941939            distances = <unsigned short **>sage_malloc(sizeof(unsigned short *)*N)
    942940            if distances == NULL:
    943941                sage_free(_distances_)