Coverage Report

Created: 2025-10-10 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libzip/lib/zip_error_strerror.c
Line
Count
Source
1
/*
2
  zip_error_sterror.c -- get string representation of struct zip_error
3
  Copyright (C) 1999-2023 Dieter Baron and Thomas Klausner
4
5
  This file is part of libzip, a library to manipulate ZIP archives.
6
  The authors can be contacted at <info@libzip.org>
7
8
  Redistribution and use in source and binary forms, with or without
9
  modification, are permitted provided that the following conditions
10
  are met:
11
  1. Redistributions of source code must retain the above copyright
12
     notice, this list of conditions and the following disclaimer.
13
  2. Redistributions in binary form must reproduce the above copyright
14
     notice, this list of conditions and the following disclaimer in
15
     the documentation and/or other materials provided with the
16
     distribution.
17
  3. The names of the authors may not be used to endorse or promote
18
     products derived from this software without specific prior
19
     written permission.
20
21
  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22
  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27
  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29
  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31
  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <zlib.h>
39
40
#include "zipint.h"
41
42
ZIP_EXTERN const char *
43
51.3k
zip_error_strerror(zip_error_t *err) {
44
51.3k
    const char *zip_error_string, *system_error_string;
45
51.3k
    char *s;
46
51.3k
    char *system_error_buffer = NULL;
47
48
51.3k
    zip_error_fini(err);
49
50
51.3k
    if (err->zip_err < 0 || err->zip_err >= _zip_err_str_count) {
51
0
        system_error_buffer = (char *)malloc(128);
52
0
        if (system_error_buffer == NULL) {
53
0
            return _zip_err_str[ZIP_ER_MEMORY].description;
54
0
        }
55
0
        snprintf_s(system_error_buffer, 128, "Unknown error %d", err->zip_err);
56
0
        system_error_buffer[128 - 1] = '\0'; /* make sure string is NUL-terminated */
57
0
        zip_error_string = NULL;
58
0
        system_error_string = system_error_buffer;
59
0
    }
60
51.3k
    else {
61
51.3k
        zip_error_string = _zip_err_str[err->zip_err].description;
62
63
51.3k
        switch (_zip_err_str[err->zip_err].type) {
64
231
            case ZIP_ET_SYS: {
65
231
                size_t len = strerrorlen_s(err->sys_err) + 1;
66
231
                system_error_buffer = malloc(len);
67
231
                if (system_error_buffer == NULL) {
68
0
                    return _zip_err_str[ZIP_ER_MEMORY].description;
69
0
                }
70
231
                strerror_s(system_error_buffer, len, err->sys_err);
71
231
                system_error_string = system_error_buffer;
72
231
                break;
73
231
            }
74
                
75
0
            case ZIP_ET_ZLIB:
76
0
                system_error_string = zError(err->sys_err);
77
0
                break;
78
                
79
3.35k
            case ZIP_ET_LIBZIP: {
80
3.35k
                zip_uint8_t error = GET_ERROR_FROM_DETAIL(err->sys_err);
81
3.35k
                int index = GET_INDEX_FROM_DETAIL(err->sys_err);
82
                
83
3.35k
                if (error == 0) {
84
0
                    system_error_string = NULL;
85
0
                }
86
3.35k
                else if (error >= _zip_err_details_count) {
87
0
                    system_error_buffer = (char *)malloc(128);
88
0
                    if (system_error_buffer == NULL) {
89
0
                        return _zip_err_str[ZIP_ER_MEMORY].description;
90
0
                    }
91
0
                    snprintf_s(system_error_buffer, 128, "invalid detail error %u", error);
92
0
                    system_error_buffer[128 - 1] = '\0'; /* make sure string is NUL-terminated */
93
0
                    system_error_string = system_error_buffer;
94
0
                }
95
3.35k
                else if (_zip_err_details[error].type == ZIP_DETAIL_ET_ENTRY && index < MAX_DETAIL_INDEX) {
96
1.30k
                    system_error_buffer = (char *)malloc(128);
97
1.30k
                    if (system_error_buffer == NULL) {
98
0
                        return _zip_err_str[ZIP_ER_MEMORY].description;
99
0
                    }
100
1.30k
                    snprintf_s(system_error_buffer, 128, "entry %d: %s", index, _zip_err_details[error].description);
101
1.30k
                    system_error_buffer[128 - 1] = '\0'; /* make sure string is NUL-terminated */
102
1.30k
                    system_error_string = system_error_buffer;
103
1.30k
                }
104
2.04k
                else {
105
2.04k
                    system_error_string = _zip_err_details[error].description;
106
2.04k
                }
107
3.35k
                break;
108
3.35k
            }
109
                
110
47.7k
            default:
111
47.7k
                system_error_string = NULL;
112
51.3k
        }
113
51.3k
    }
114
115
51.3k
    if (system_error_string == NULL) {
116
47.7k
        free(system_error_buffer);
117
47.7k
        return zip_error_string;
118
47.7k
    }
119
3.58k
    else {
120
3.58k
        size_t length = strlen(system_error_string);
121
3.58k
        if (zip_error_string) {
122
3.58k
            size_t length_error = strlen(zip_error_string);
123
3.58k
            if (length + length_error + 2 < length) {
124
0
                free(system_error_buffer);
125
0
                return _zip_err_str[ZIP_ER_MEMORY].description;
126
0
            }
127
3.58k
            length += length_error + 2;
128
3.58k
        }
129
3.58k
        if (length == SIZE_MAX || (s = (char *)malloc(length + 1)) == NULL) {
130
0
            free(system_error_buffer);
131
0
            return _zip_err_str[ZIP_ER_MEMORY].description;
132
0
        }
133
134
3.58k
        snprintf_s(s, length + 1, "%s%s%s", (zip_error_string ? zip_error_string : ""), (zip_error_string ? ": " : ""), system_error_string);
135
3.58k
        err->str = s;
136
137
3.58k
        free(system_error_buffer);
138
3.58k
        return s;
139
3.58k
    }
140
51.3k
}