Coverage Report

Created: 2026-04-12 06:41

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-2025 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 "zipint.h"
36
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <zlib.h>
41
42
57.3k
ZIP_EXTERN const char *zip_error_strerror(zip_error_t *err) {
43
57.3k
    const char *zip_error_string, *system_error_string;
44
57.3k
    char *s;
45
57.3k
    char *system_error_buffer = NULL;
46
47
57.3k
    zip_error_fini(err);
48
49
57.3k
    if (err->zip_err < 0 || err->zip_err >= _zip_err_str_count) {
50
0
        system_error_buffer = (char *)malloc(128);
51
0
        if (system_error_buffer == NULL) {
52
0
            return _zip_err_str[ZIP_ER_MEMORY].description;
53
0
        }
54
0
        snprintf_s(system_error_buffer, 128, "Unknown error %d", err->zip_err);
55
0
        system_error_buffer[128 - 1] = '\0'; /* make sure string is NUL-terminated */
56
0
        zip_error_string = NULL;
57
0
        system_error_string = system_error_buffer;
58
0
    }
59
57.3k
    else {
60
57.3k
        zip_error_string = _zip_err_str[err->zip_err].description;
61
62
57.3k
        switch (_zip_err_str[err->zip_err].type) {
63
264
        case ZIP_ET_SYS: {
64
264
            size_t len = strerrorlen_s(err->sys_err) + 1;
65
264
            system_error_buffer = malloc(len);
66
264
            if (system_error_buffer == NULL) {
67
0
                return _zip_err_str[ZIP_ER_MEMORY].description;
68
0
            }
69
264
            strerror_s(system_error_buffer, len, err->sys_err);
70
264
            system_error_string = system_error_buffer;
71
264
            break;
72
264
        }
73
74
0
        case ZIP_ET_ZLIB:
75
0
            system_error_string = zError(err->sys_err);
76
0
            break;
77
78
3.38k
        case ZIP_ET_LIBZIP: {
79
3.38k
            zip_uint8_t error = GET_ERROR_FROM_DETAIL(err->sys_err);
80
3.38k
            int index = GET_INDEX_FROM_DETAIL(err->sys_err);
81
82
3.38k
            if (error == 0) {
83
0
                system_error_string = NULL;
84
0
            }
85
3.38k
            else if (error >= _zip_err_details_count) {
86
0
                system_error_buffer = (char *)malloc(128);
87
0
                if (system_error_buffer == NULL) {
88
0
                    return _zip_err_str[ZIP_ER_MEMORY].description;
89
0
                }
90
0
                snprintf_s(system_error_buffer, 128, "invalid detail error %u", error);
91
0
                system_error_buffer[128 - 1] = '\0'; /* make sure string is NUL-terminated */
92
0
                system_error_string = system_error_buffer;
93
0
            }
94
3.38k
            else if (_zip_err_details[error].type == ZIP_DETAIL_ET_ENTRY && index < MAX_DETAIL_INDEX) {
95
1.43k
                system_error_buffer = (char *)malloc(128);
96
1.43k
                if (system_error_buffer == NULL) {
97
0
                    return _zip_err_str[ZIP_ER_MEMORY].description;
98
0
                }
99
1.43k
                snprintf_s(system_error_buffer, 128, "entry %d: %s", index, _zip_err_details[error].description);
100
1.43k
                system_error_buffer[128 - 1] = '\0'; /* make sure string is NUL-terminated */
101
1.43k
                system_error_string = system_error_buffer;
102
1.43k
            }
103
1.95k
            else {
104
1.95k
                system_error_string = _zip_err_details[error].description;
105
1.95k
            }
106
3.38k
            break;
107
3.38k
        }
108
109
53.7k
        default:
110
53.7k
            system_error_string = NULL;
111
57.3k
        }
112
57.3k
    }
113
114
57.3k
    if (system_error_string == NULL) {
115
53.7k
        free(system_error_buffer);
116
53.7k
        return zip_error_string;
117
53.7k
    }
118
3.64k
    else {
119
3.64k
        size_t length = strlen(system_error_string);
120
3.64k
        if (zip_error_string) {
121
3.64k
            size_t length_error = strlen(zip_error_string);
122
3.64k
            if (length + length_error + 2 < length) {
123
0
                free(system_error_buffer);
124
0
                return _zip_err_str[ZIP_ER_MEMORY].description;
125
0
            }
126
3.64k
            length += length_error + 2;
127
3.64k
        }
128
3.64k
        if (length == SIZE_MAX || (s = (char *)malloc(length + 1)) == NULL) {
129
0
            free(system_error_buffer);
130
0
            return _zip_err_str[ZIP_ER_MEMORY].description;
131
0
        }
132
133
3.64k
        snprintf_s(s, length + 1, "%s%s%s", (zip_error_string ? zip_error_string : ""), (zip_error_string ? ": " : ""), system_error_string);
134
3.64k
        err->str = s;
135
136
3.64k
        free(system_error_buffer);
137
3.64k
        return s;
138
3.64k
    }
139
57.3k
}