Coverage Report

Created: 2025-07-01 06:58

/src/sleuthkit/tsk/base/mymalloc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * The Sleuth Kit
3
 *
4
 *
5
 * Brian Carrier [carrier <at> sleuthkit [dot] org]
6
 * Copyright (c) 2006-2011 Brian Carrier, Basis Technology.  All rights reserved.
7
 */
8
9
/** \file mymalloc.c
10
 * These functions allocate and reallocate memory and set the error handling functions
11
 * when an error occurs.
12
 */
13
14
/*  The IBM Public License must be distributed with this software.
15
* AUTHOR(S)
16
* Wietse Venema
17
* IBM T.J. Watson Research
18
* P.O. Box 704
19
* Yorktown Heights, NY 10598, USA
20
*--*/
21
22
#include "tsk_base_i.h"
23
#include <errno.h>
24
25
/* tsk_malloc - allocate and zero memory and set error values on error
26
 */
27
void *
28
tsk_malloc(size_t len)
29
352
{
30
352
    void *ptr;
31
32
352
    if ((ptr = calloc(len, 1)) == 0) {
33
0
        tsk_error_reset();
34
0
        tsk_error_set_errno(TSK_ERR_AUX_MALLOC);
35
0
        tsk_error_set_errstr("tsk_malloc: %s (%" PRIuSIZE" requested)", strerror(errno), len);
36
0
    }
37
38
352
    return ptr;
39
352
}
40
41
/* tsk_realloc - reallocate memory and set error values if needed */
42
void *
43
tsk_realloc(void *ptr, size_t len)
44
0
{
45
    // Use tmpPtr to prevent memory leak when realloc failed
46
0
    void *tmpPtr = realloc(ptr, len);
47
0
    if (tmpPtr == 0) {
48
0
        tsk_error_reset();
49
0
        tsk_error_set_errno(TSK_ERR_AUX_MALLOC);
50
0
        tsk_error_set_errstr("tsk_realloc: %s (%" PRIuSIZE" requested)", strerror(errno), len);
51
0
        return (void *)0;
52
0
    }
53
0
    else {
54
0
        ptr = tmpPtr;
55
0
    }
56
0
    return ptr;
57
0
}