Coverage Report

Created: 2026-04-12 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtiff/libtiff/tif_unix.c
Line
Count
Source
1
/*
2
 * Copyright (c) 1988-1997 Sam Leffler
3
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software and
6
 * its documentation for any purpose is hereby granted without fee, provided
7
 * that (i) the above copyright notices and this permission notice appear in
8
 * all copies of the software and related documentation, and (ii) the names of
9
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10
 * publicity relating to the software without the specific, prior written
11
 * permission of Sam Leffler and Silicon Graphics.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22
 * OF THIS SOFTWARE.
23
 */
24
25
/*
26
 * TIFF Library UNIX-specific Routines. These are should also work with the
27
 * Windows Common RunTime Library.
28
 */
29
30
#ifdef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS
31
#undef TIFF_DO_NOT_USE_NON_EXT_ALLOC_FUNCTIONS
32
#endif
33
34
#include "tif_config.h"
35
36
#ifdef HAVE_SYS_TYPES_H
37
#include <sys/types.h>
38
#endif
39
40
#include <errno.h>
41
42
#include <stdarg.h>
43
#include <stdlib.h>
44
#include <sys/stat.h>
45
46
#ifdef HAVE_UNISTD_H
47
#include <unistd.h>
48
#endif
49
50
#ifdef HAVE_FCNTL_H
51
#include <fcntl.h>
52
#endif
53
54
#ifdef HAVE_IO_H
55
#include <io.h>
56
#endif
57
58
#include "tiffiop.h"
59
60
0
#define TIFF_IO_MAX 2147483647U
61
62
typedef union fd_as_handle_union
63
{
64
    int fd;
65
    thandle_t h;
66
} fd_as_handle_union_t;
67
68
static tmsize_t _tiffReadProc(thandle_t fd, void *buf, tmsize_t size)
69
0
{
70
0
    fd_as_handle_union_t fdh;
71
0
    const size_t bytes_total = (size_t)size;
72
0
    size_t bytes_read;
73
0
    tmsize_t count = -1;
74
0
    if ((tmsize_t)bytes_total != size)
75
0
    {
76
0
        errno = EINVAL;
77
0
        return (tmsize_t)-1;
78
0
    }
79
0
    fdh.h = fd;
80
0
    for (bytes_read = 0; bytes_read < bytes_total; bytes_read += (size_t)count)
81
0
    {
82
0
        char *buf_offset = (char *)buf + bytes_read;
83
0
        size_t io_size = (size_t)(bytes_total - bytes_read);
84
0
        if (io_size > TIFF_IO_MAX)
85
0
            io_size = TIFF_IO_MAX;
86
        /* Below is an obvious false positive of Coverity Scan */
87
        /* coverity[overflow_sink] */
88
0
        count = read(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
89
0
        if (count <= 0)
90
0
            break;
91
0
    }
92
0
    if (count < 0)
93
0
        return (tmsize_t)-1;
94
    /* Silence Coverity Scan warning about unsigned to signed underflow. */
95
    /* coverity[return_overflow:SUPPRESS] */
96
0
    return (tmsize_t)bytes_read;
97
0
}
98
99
static tmsize_t _tiffWriteProc(thandle_t fd, void *buf, tmsize_t size)
100
0
{
101
0
    fd_as_handle_union_t fdh;
102
0
    const size_t bytes_total = (size_t)size;
103
0
    size_t bytes_written;
104
0
    tmsize_t count = -1;
105
0
    if ((tmsize_t)bytes_total != size)
106
0
    {
107
0
        errno = EINVAL;
108
0
        return (tmsize_t)-1;
109
0
    }
110
0
    fdh.h = fd;
111
0
    for (bytes_written = 0; bytes_written < bytes_total;
112
0
         bytes_written += (size_t)count)
113
0
    {
114
0
        const char *buf_offset = (char *)buf + bytes_written;
115
0
        size_t io_size = (size_t)(bytes_total - bytes_written);
116
0
        if (io_size > TIFF_IO_MAX)
117
0
            io_size = TIFF_IO_MAX;
118
        /* Below is an obvious false positive of Coverity Scan */
119
        /* coverity[overflow_sink] */
120
0
        count = write(fdh.fd, buf_offset, (TIFFIOSize_t)io_size);
121
0
        if (count <= 0)
122
0
            break;
123
0
    }
124
0
    if (count < 0)
125
0
        return (tmsize_t)-1;
126
    /* Silence Coverity Scan warning about unsigned to signed underflow. */
127
    /* coverity[return_overflow:SUPPRESS] */
128
0
    return (tmsize_t)bytes_written;
129
    /* return ((tmsize_t) write(fdh.fd, buf, bytes_total)); */
130
0
}
131
132
static uint64_t _tiffSeekProc(thandle_t fd, uint64_t off, int whence)
133
0
{
134
0
    fd_as_handle_union_t fdh;
135
0
    _TIFF_off_t off_io = (_TIFF_off_t)off;
136
0
    if ((uint64_t)off_io != off)
137
0
    {
138
0
        errno = EINVAL;
139
0
        return (uint64_t)-1; /* this is really gross */
140
0
    }
141
0
    fdh.h = fd;
142
0
    return ((uint64_t)_TIFF_lseek_f(fdh.fd, off_io, whence));
143
0
}
144
145
static int _tiffCloseProc(thandle_t fd)
146
0
{
147
0
    fd_as_handle_union_t fdh;
148
0
    fdh.h = fd;
149
0
    return (close(fdh.fd));
150
0
}
151
152
static uint64_t _tiffSizeProc(thandle_t fd)
153
0
{
154
0
    _TIFF_stat_s sb;
155
0
    fd_as_handle_union_t fdh;
156
0
    fdh.h = fd;
157
0
    if (_TIFF_fstat_f(fdh.fd, &sb) < 0)
158
0
        return (0);
159
0
    else
160
0
        return ((uint64_t)sb.st_size);
161
0
}
162
163
#ifdef HAVE_MMAP
164
#include <sys/mman.h>
165
166
static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
167
0
{
168
0
    uint64_t size64 = _tiffSizeProc(fd);
169
0
    tmsize_t sizem = (tmsize_t)size64;
170
0
    if (size64 && (uint64_t)sizem == size64)
171
0
    {
172
0
        fd_as_handle_union_t fdh;
173
0
        fdh.h = fd;
174
0
        *pbase =
175
0
            (void *)mmap(0, (size_t)sizem, PROT_READ, MAP_SHARED, fdh.fd, 0);
176
0
        if (*pbase != (void *)-1)
177
0
        {
178
0
            *psize = (toff_t)sizem;
179
0
            return (1);
180
0
        }
181
0
    }
182
0
    return (0);
183
0
}
184
185
static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
186
0
{
187
0
    (void)fd;
188
0
    (void)munmap(base, (size_t)size);
189
0
}
190
#else  /* !HAVE_MMAP */
191
static int _tiffMapProc(thandle_t fd, void **pbase, toff_t *psize)
192
{
193
    (void)fd;
194
    (void)pbase;
195
    (void)psize;
196
    return (0);
197
}
198
199
static void _tiffUnmapProc(thandle_t fd, void *base, toff_t size)
200
{
201
    (void)fd;
202
    (void)base;
203
    (void)size;
204
}
205
#endif /* !HAVE_MMAP */
206
207
/*
208
 * Open a TIFF file descriptor for read/writing.
209
 */
210
TIFF *TIFFFdOpen(int fd, const char *name, const char *mode)
211
0
{
212
0
    return TIFFFdOpenExt(fd, name, mode, NULL);
213
0
}
214
215
TIFF *TIFFFdOpenExt(int fd, const char *name, const char *mode,
216
                    TIFFOpenOptions *opts)
217
0
{
218
0
    TIFF *tif;
219
220
0
    fd_as_handle_union_t fdh;
221
0
    fdh.fd = fd;
222
0
    tif = TIFFClientOpenExt(name, mode, fdh.h, _tiffReadProc, _tiffWriteProc,
223
0
                            _tiffSeekProc, _tiffCloseProc, _tiffSizeProc,
224
0
                            _tiffMapProc, _tiffUnmapProc, opts);
225
0
    if (tif)
226
0
        tif->tif_fd = fd;
227
0
    return (tif);
228
0
}
229
230
/*
231
 * Open a TIFF file for read/writing.
232
 */
233
TIFF *TIFFOpen(const char *name, const char *mode)
234
0
{
235
0
    return TIFFOpenExt(name, mode, NULL);
236
0
}
237
238
TIFF *TIFFOpenExt(const char *name, const char *mode, TIFFOpenOptions *opts)
239
0
{
240
0
    static const char module[] = "TIFFOpen";
241
0
    int m, fd;
242
0
    TIFF *tif;
243
244
0
    m = _TIFFgetMode(opts, NULL, mode, module);
245
0
    if (m == -1)
246
0
        return ((TIFF *)0);
247
248
/* for cygwin and mingw */
249
#ifdef O_BINARY
250
    m |= O_BINARY;
251
#endif
252
253
0
    fd = open(name, m, 0666);
254
0
    if (fd < 0)
255
0
    {
256
0
        if (errno > 0 && strerror(errno) != NULL)
257
0
        {
258
0
            _TIFFErrorEarly(opts, NULL, module, "%s: %s", name,
259
0
                            strerror(errno));
260
0
        }
261
0
        else
262
0
        {
263
0
            _TIFFErrorEarly(opts, NULL, module, "%s: Cannot open", name);
264
0
        }
265
0
        return ((TIFF *)0);
266
0
    }
267
268
0
    tif = TIFFFdOpenExt((int)fd, name, mode, opts);
269
0
    if (!tif)
270
0
        close(fd);
271
0
    return tif;
272
0
}
273
274
#ifdef _WIN32
275
#include <windows.h>
276
/*
277
 * Open a TIFF file with a Unicode filename, for read/writing.
278
 */
279
TIFF *TIFFOpenW(const wchar_t *name, const char *mode)
280
{
281
    return TIFFOpenWExt(name, mode, NULL);
282
}
283
TIFF *TIFFOpenWExt(const wchar_t *name, const char *mode, TIFFOpenOptions *opts)
284
{
285
    static const char module[] = "TIFFOpenW";
286
    int m, fd;
287
    int mbsize;
288
    char *mbname;
289
    TIFF *tif;
290
291
    m = _TIFFgetMode(opts, NULL, mode, module);
292
    if (m == -1)
293
        return ((TIFF *)0);
294
295
/* for cygwin and mingw */
296
#ifdef O_BINARY
297
    m |= O_BINARY;
298
#endif
299
300
    fd = _wopen(name, m, 0666);
301
    if (fd < 0)
302
    {
303
        _TIFFErrorEarly(opts, NULL, module, "%ls: Cannot open", name);
304
        return ((TIFF *)0);
305
    }
306
307
    mbname = NULL;
308
    mbsize = WideCharToMultiByte(CP_ACP, 0, name, -1, NULL, 0, NULL, NULL);
309
    if (mbsize > 0)
310
    {
311
        mbname = _TIFFmalloc(mbsize);
312
        if (!mbname)
313
        {
314
            _TIFFErrorEarly(
315
                opts, NULL, module,
316
                "Can't allocate space for filename conversion buffer");
317
            return ((TIFF *)0);
318
        }
319
320
        WideCharToMultiByte(CP_ACP, 0, name, -1, mbname, mbsize, NULL, NULL);
321
    }
322
323
    tif = TIFFFdOpenExt((int)fd, (mbname != NULL) ? mbname : "<unknown>", mode,
324
                        opts);
325
326
    _TIFFfree(mbname);
327
328
    if (!tif)
329
        close(fd);
330
    return tif;
331
}
332
#endif
333
334
void *_TIFFmalloc(tmsize_t s)
335
2.93M
{
336
2.93M
    if (s == 0)
337
0
        return ((void *)NULL);
338
339
2.93M
    return (malloc((size_t)s));
340
2.93M
}
341
342
void *_TIFFcalloc(tmsize_t nmemb, tmsize_t siz)
343
1.81k
{
344
1.81k
    if (nmemb == 0 || siz == 0)
345
0
        return ((void *)NULL);
346
347
1.81k
    return calloc((size_t)nmemb, (size_t)siz);
348
1.81k
}
349
350
3.05M
void _TIFFfree(void *p) { free(p); }
351
352
289k
void *_TIFFrealloc(void *p, tmsize_t s) { return (realloc(p, (size_t)s)); }
353
354
352k
void _TIFFmemset(void *p, int v, tmsize_t c) { memset(p, v, (size_t)c); }
355
356
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
357
86.4k
{
358
86.4k
    memcpy(d, s, (size_t)c);
359
86.4k
}
360
361
int _TIFFmemcmp(const void *p1, const void *p2, tmsize_t c)
362
0
{
363
0
    return (memcmp(p1, p2, (size_t)c));
364
0
}
365
366
static void TIFF_ATTRIBUTE((__format__(__printf__, 2, 0)))
367
    unixWarningHandler(const char *module, const char *fmt, va_list ap)
368
0
{
369
0
    if (module != NULL)
370
0
        fprintf(stderr, "%s: ", module);
371
0
    fprintf(stderr, "Warning, ");
372
0
    vfprintf(stderr, fmt, ap);
373
0
    fprintf(stderr, ".\n");
374
0
}
375
TIFFErrorHandler _TIFFwarningHandler = unixWarningHandler;
376
377
static void TIFF_ATTRIBUTE((__format__(__printf__, 2, 0)))
378
    unixErrorHandler(const char *module, const char *fmt, va_list ap)
379
0
{
380
0
    if (module != NULL)
381
0
        fprintf(stderr, "%s: ", module);
382
0
    vfprintf(stderr, fmt, ap);
383
    fprintf(stderr, ".\n");
384
0
}
385
TIFFErrorHandler _TIFFerrorHandler = unixErrorHandler;