Path: blob/master/thirdparty/pcre2/src/pcre2_chkdint.c
9903 views
/*************************************************1* Perl-Compatible Regular Expressions *2*************************************************/34/* PCRE is a library of functions to support regular expressions whose syntax5and semantics are as close as possible to those of the Perl 5 language.67Written by Philip Hazel8Copyright (c) 2023 University of Cambridge910-----------------------------------------------------------------------------11Redistribution and use in source and binary forms, with or without12modification, are permitted provided that the following conditions are met:1314* Redistributions of source code must retain the above copyright notice,15this list of conditions and the following disclaimer.1617* Redistributions in binary form must reproduce the above copyright18notice, this list of conditions and the following disclaimer in the19documentation and/or other materials provided with the distribution.2021* Neither the name of the University of Cambridge nor the names of its22contributors may be used to endorse or promote products derived from23this software without specific prior written permission.2425THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"26AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE27IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE28ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE29LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR30CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF31SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS32INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN33CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)34ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE35POSSIBILITY OF SUCH DAMAGE.36-----------------------------------------------------------------------------37*/3839/* This file contains functions to implement checked integer operation */4041#ifndef PCRE2_PCRE2TEST42#ifdef HAVE_CONFIG_H43#include "config.h"44#endif4546#include "pcre2_internal.h"47#endif4849/*************************************************50* Checked Integer Multiplication *51*************************************************/5253/*54Arguments:55r A pointer to PCRE2_SIZE to store the answer56a, b Two integers5758Returns: Bool indicating if the operation overflows5960It is modeled after C23's <stdckdint.h> interface61The INT64_OR_DOUBLE type is a 64-bit integer type when available,62otherwise double. */6364BOOL65PRIV(ckd_smul)(PCRE2_SIZE *r, int a, int b)66{67#ifdef HAVE_BUILTIN_MUL_OVERFLOW68PCRE2_SIZE m;6970if (__builtin_mul_overflow(a, b, &m)) return TRUE;7172*r = m;73#else74INT64_OR_DOUBLE m;7576PCRE2_ASSERT(a >= 0 && b >= 0);7778m = (INT64_OR_DOUBLE)a * (INT64_OR_DOUBLE)b;7980#if defined INT64_MAX || defined int64_t81if (sizeof(m) > sizeof(*r) && m > (INT64_OR_DOUBLE)PCRE2_SIZE_MAX) return TRUE;82*r = (PCRE2_SIZE)m;83#else84if (m > PCRE2_SIZE_MAX) return TRUE;85*r = m;86#endif8788#endif8990return FALSE;91}9293/* End of pcre2_chkdint.c */949596