src/ucx.c

Sun, 21 Jan 2018 10:13:21 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 21 Jan 2018 10:13:21 +0100
changeset 270
3d80d425543b
parent 263
3ff0abc49ed5
child 273
9c1591b3c4a4
permissions
-rw-r--r--

adds integer overflow checks

     1 /**
     2  * @mainpage UAP Common Extensions
     3  * Library with common and useful functions, macros and data structures.
     4  * <p>
     5  * Latest available source:<br>
     6  * <a href="https://sourceforge.net/projects/ucx/files/">
     7  * https://sourceforge.net/projects/ucx/files/</a>
     8  * </p>
     9  * 
    10  * <p>
    11  * Repositories:<br>
    12  * <a href="https://sourceforge.net/p/ucx/code">
    13  * https://sourceforge.net/p/ucx/code</a>
    14  * -&nbsp;or&nbsp;-
    15  * <a href="https://develop.uap-core.de/hg/ucx">
    16  * https://develop.uap-core.de/hg/ucx</a>
    17  * </p>
    18  * 
    19  * <h2>LICENCE</h2>
    20  * 
    21  * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
    22  *
    23  * Redistribution and use in source and binary forms, with or without
    24  * modification, are permitted provided that the following conditions are met:
    25  *
    26  *   1. Redistributions of source code must retain the above copyright
    27  *      notice, this list of conditions and the following disclaimer.
    28  *
    29  *   2. Redistributions in binary form must reproduce the above copyright
    30  *      notice, this list of conditions and the following disclaimer in the
    31  *      documentation and/or other materials provided with the distribution.
    32  *
    33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    34  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    36  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    37  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    38  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    39  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    40  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    41  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    42  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    43  * POSSIBILITY OF SUCH DAMAGE.
    44  */
    46 #include "ucx/ucx.h"
    48 #ifndef UCX_MUL_BUILTIN
    49 int ucx_szmul(size_t a, size_t b, size_t *result) {
    50     if(a == 0 || b == 0) {
    51         *result = 0;
    52         return 1;
    53     }
    54     size_t r = a * b;
    55     if(r / b == a) {
    56         *result = r;
    57         return 0;
    58     } else {
    59         *result = 0;
    60         return 1;
    61     }
    62 }
    63 #endif

mercurial