cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

realloc_or_die.c (3694B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include "assert-signal.h"
     21
     22#include <CUnit/CUnit.h>
     23#include <guacamole/mem.h>
     24#include <stdint.h>
     25
     26/**
     27 * Test which verifies that guac_mem_realloc_or_die() returns NULL for all
     28 * inputs involving at least one zero value (reallocation to zero bytes is not
     29 * an error but equivalent freeing the memory).
     30 */
     31void test_mem__realloc_or_die_success_zero() {
     32
     33    void* ptr;
     34
     35    ptr = guac_mem_alloc(1);
     36    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     37    CU_ASSERT_PTR_NULL(guac_mem_realloc_or_die(ptr, 0));
     38
     39    ptr = guac_mem_alloc(1);
     40    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     41    CU_ASSERT_PTR_NULL(guac_mem_realloc_or_die(ptr, 0, 0));
     42
     43    ptr = guac_mem_alloc(1);
     44    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     45    CU_ASSERT_PTR_NULL(guac_mem_realloc_or_die(ptr, 0, 0, 0));
     46
     47    ptr = guac_mem_alloc(1);
     48    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     49    CU_ASSERT_PTR_NULL(guac_mem_realloc_or_die(ptr, 0, 0, 0, 0));
     50
     51    ptr = guac_mem_alloc(1);
     52    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     53    CU_ASSERT_PTR_NULL(guac_mem_realloc_or_die(ptr, 0, 0, 0, 0, 0));
     54
     55    ptr = guac_mem_alloc(1);
     56    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     57    CU_ASSERT_PTR_NULL(guac_mem_realloc_or_die(ptr, 1, 0));
     58
     59    ptr = guac_mem_alloc(1);
     60    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     61    CU_ASSERT_PTR_NULL(guac_mem_realloc_or_die(ptr, 3, 2, 0));
     62
     63    ptr = guac_mem_alloc(1);
     64    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     65    CU_ASSERT_PTR_NULL(guac_mem_realloc_or_die(ptr, 5, 0, 8, 9));
     66
     67    ptr = guac_mem_alloc(1);
     68    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     69    CU_ASSERT_PTR_NULL(guac_mem_realloc_or_die(ptr, 99, 99, 99, 0, 99));
     70
     71}
     72
     73/**
     74 * Test which verifies that guac_mem_realloc_or_die() successfully allocates
     75 * blocks of memory for inputs that can reasonably be expected to succeed.
     76 */
     77void test_mem__realloc_or_die_success() {
     78
     79    void* ptr;
     80
     81    ptr = guac_mem_alloc(1);
     82    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     83    ptr = guac_mem_realloc_or_die(ptr, 123);
     84    CU_ASSERT_PTR_NOT_NULL(ptr);
     85    guac_mem_free(ptr);
     86
     87    ptr = guac_mem_alloc(1);
     88    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     89    ptr = guac_mem_realloc_or_die(ptr, 123, 456);
     90    CU_ASSERT_PTR_NOT_NULL(ptr);
     91    guac_mem_free(ptr);
     92
     93    ptr = guac_mem_alloc(1);
     94    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
     95    ptr = guac_mem_realloc_or_die(ptr, 123, 456, 789);
     96    CU_ASSERT_PTR_NOT_NULL(ptr);
     97    guac_mem_free(ptr);
     98
     99}
    100
    101/**
    102 * Test which verifies that guac_mem_realloc_or_die() fails to allocate blocks of
    103 * memory that exceed the capacity of a size_t.
    104 */
    105void test_mem__realloc_or_die_fail_large() {
    106
    107    void* ptr;
    108
    109    ptr = guac_mem_alloc(1);
    110    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
    111    ASSERT_SIGNALLED(SIGABRT, guac_mem_realloc_or_die(ptr, 123, 456, SIZE_MAX));
    112    guac_mem_free(ptr);
    113
    114    ptr = guac_mem_alloc(1);
    115    CU_ASSERT_PTR_NOT_NULL_FATAL(ptr);
    116    ASSERT_SIGNALLED(SIGABRT, guac_mem_realloc_or_die(ptr, SIZE_MAX / 2, SIZE_MAX / 2));
    117    guac_mem_free(ptr);
    118
    119}
    120