• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stefanberger / swtpm / #2962

04 May 2026 04:12PM UTC coverage: 73.671% (+0.5%) from 73.207%
#2962

push

travis-ci

web-flow
Merge 238e29932 into 60daa0b0d

8022 of 10889 relevant lines covered (73.67%)

9890.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.02
/src/swtpm/swtpm_debug.c
1
/********************************************************************************/
2
/*                                                                              */
3
/*                         TPM Debug Utilities                                  */
4
/*                           Written by Ken Goldman                             */
5
/*                     IBM Thomas J. Watson Research Center                     */
6
/*            $Id: tpm_debug.c 4179 2010-11-10 20:10:24Z kgoldman $             */
7
/*                                                                              */
8
/* (c) Copyright IBM Corporation 2006, 2010.                                        */
9
/*                                                                                */
10
/* All rights reserved.                                                                */
11
/*                                                                                 */
12
/* Redistribution and use in source and binary forms, with or without                */
13
/* modification, are permitted provided that the following conditions are        */
14
/* met:                                                                                */
15
/*                                                                                 */
16
/* Redistributions of source code must retain the above copyright notice,        */
17
/* this list of conditions and the following disclaimer.                        */
18
/*                                                                                 */
19
/* Redistributions in binary form must reproduce the above copyright                */
20
/* notice, this list of conditions and the following disclaimer in the                */
21
/* documentation and/or other materials provided with the distribution.                */
22
/*                                                                                 */
23
/* Neither the names of the IBM Corporation nor the names of its                */
24
/* contributors may be used to endorse or promote products derived from                */
25
/* this software without specific prior written permission.                        */
26
/*                                                                                 */
27
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS                */
28
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT                */
29
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR        */
30
/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
31
/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,        */
32
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT                */
33
/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,        */
34
/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY        */
35
/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT                */
36
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE        */
37
/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
38
/********************************************************************************/
39

40
#include <config.h>
41

42
#define _GNU_SOURCE
43
#include <stdlib.h>
44
#include <stdio.h>
45
#include <stdint.h>
46
#include <string.h>
47
#include <stdarg.h>
48

49
#include "swtpm_debug.h"
50
#include "logging.h"
51
#include "compiler_dependencies.h"
52

53
static int SWTPM_AppendPrintf(char **buffer, const char *fmt, ...)
54
    SWTPM_ATTRIBUTE_FORMAT(2, 3);
55

56
/*
57
 * SWTPM_AppendPrintf() print and append to buffer
58
 *
59
 * @buffer: pointer to existing buffer or pointer to NULL to start a new buffer
60
 * @fmt: typical printf fmt
61
 * @...: varagr printf parameters
62
 *
63
 */
64
static int SWTPM_AppendPrintf(char **buffer, const char *fmt, ...)
387,268✔
65
{
66
    va_list ap;
387,268✔
67
    int n, len = 0;
387,268✔
68
    char *dest = NULL, *nbuffer;
387,268✔
69

70
    va_start(ap, fmt);
387,268✔
71
    n = vasprintf(&dest, fmt, ap);
387,268✔
72
    va_end(ap);
387,268✔
73
    if (n < 0)
387,268✔
74
        return n;
75

76
    if (*buffer)
387,268✔
77
        len = strlen(*buffer);
363,136✔
78

79
    nbuffer = malloc(len + n + 1);
387,268✔
80
    if (!nbuffer) {
387,268✔
81
        free(dest);
×
82
        return -1;
×
83
    }
84
    if (*buffer)
387,268✔
85
        memcpy(nbuffer, *buffer, len);
363,136✔
86
    memcpy(&nbuffer[len], dest, n);
387,268✔
87
    nbuffer[len + n] = 0;
387,268✔
88

89
    free(dest);
387,268✔
90
    free(*buffer);
387,268✔
91
    *buffer = nbuffer;
387,268✔
92

93
    return len + n;
387,268✔
94
}
95

96
/* SWTPM_PrintAll() prints 'string', the length, and then the entire byte array
97
 */
98
void SWTPM_PrintAll(const char *string, const char *indentation,
89,777✔
99
                    const unsigned char* buff, uint32_t length)
100
{
101
    uint32_t i;
89,777✔
102
    int indent;
89,777✔
103
    char *linebuffer = NULL;
89,777✔
104

105
    indent = log_check_string(string);
89,777✔
106
    if (indent < 0)
89,777✔
107
        return;
108

109
    if (buff != NULL) {
5,436✔
110
        logprintf(STDERR_FILENO, "%s length %u\n", string, length);
5,436✔
111

112
        SWTPM_AppendPrintf(&linebuffer, "%s", indentation);
5,436✔
113
        for (i = 0 ; i < length ; i++) {
349,876✔
114
            if (i && !( i % 16 )) {
339,004✔
115
                SWTPM_AppendPrintf(&linebuffer, "\n");
18,696✔
116

117
                logprintfA(STDERR_FILENO, 0, "%s", linebuffer);
18,696✔
118

119
                free(linebuffer);
18,696✔
120
                linebuffer = NULL;
18,696✔
121
                SWTPM_AppendPrintf(&linebuffer, "%s", indentation);
18,696✔
122
            }
123

124
            SWTPM_AppendPrintf(&linebuffer, "%.2X ", buff[i]);
339,004✔
125
        }
126
        SWTPM_AppendPrintf(&linebuffer, "\n");
5,436✔
127
        logprintf(STDERR_FILENO, "%s", linebuffer);
5,436✔
128
        free(linebuffer);
5,436✔
129
    }
130
    else {
131
        logprintf(STDERR_FILENO, "%s null\n", string);
×
132
    }
133
    return;
134
}
135

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc