summaryrefslogtreecommitdiff
path: root/contrib/libcbor/src/cbor/internal/memory_utils.c
blob: bbea63cb9f068f62a2dea7d260b7205237c5bac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
 *
 * libcbor is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include "memory_utils.h"
#include "cbor/common.h"

// TODO: Consider builtins
// (https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html)

/** Highest on bit position */
size_t _cbor_highest_bit(size_t number) {
  size_t bit = 0;
  while (number != 0) {
    bit++;
    number >>= 1;
  }

  return bit;
}

bool _cbor_safe_to_multiply(size_t a, size_t b) {
  if (a <= 1 || b <= 1) return true;
  return _cbor_highest_bit(a) + _cbor_highest_bit(b) <= sizeof(size_t) * 8;
}

bool _cbor_safe_to_add(size_t a, size_t b) {
  // Unsigned integer overflow doesn't constitute UB
  size_t sum = a + b;
  return sum >= a && sum >= b;
}

size_t _cbor_safe_signaling_add(size_t a, size_t b) {
  if (a == 0 || b == 0) return 0;
  if (_cbor_safe_to_add(a, b)) return a + b;
  return 0;
}

void* _cbor_alloc_multiple(size_t item_size, size_t item_count) {
  if (_cbor_safe_to_multiply(item_size, item_count)) {
    return _cbor_malloc(item_size * item_count);
  } else {
    return NULL;
  }
}

void* _cbor_realloc_multiple(void* pointer, size_t item_size,
                             size_t item_count) {
  if (_cbor_safe_to_multiply(item_size, item_count)) {
    return _cbor_realloc(pointer, item_size * item_count);
  } else {
    return NULL;
  }
}