OpenCoverage

areadlink-with-size.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/gnulib/lib/areadlink-with-size.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* readlink wrapper to return the link name in malloc'd storage.-
2 Unlike xreadlink and xreadlink_with_size, don't ever call exit.-
3-
4 Copyright (C) 2001, 2003-2007, 2009-2018 Free Software Foundation, Inc.-
5-
6 This program is free software: you can redistribute it and/or modify-
7 it under the terms of the GNU General Public License as published by-
8 the Free Software Foundation; either version 3 of the License, or-
9 (at your option) any later version.-
10-
11 This program is distributed in the hope that it will be useful,-
12 but WITHOUT ANY WARRANTY; without even the implied warranty of-
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
14 GNU General Public License for more details.-
15-
16 You should have received a copy of the GNU General Public License-
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */-
18-
19/* Written by Jim Meyering <jim@meyering.net> */-
20-
21#include <config.h>-
22-
23#include "areadlink.h"-
24-
25#include <errno.h>-
26#include <limits.h>-
27#include <stdint.h>-
28#include <stdlib.h>-
29#include <unistd.h>-
30-
31#ifndef SSIZE_MAX-
32# define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))-
33#endif-
34-
35/* SYMLINK_MAX is used only for an initial memory-allocation sanity-
36 check, so it's OK to guess too small on hosts where there is no-
37 arbitrary limit to symbolic link length. */-
38#ifndef SYMLINK_MAX-
39# define SYMLINK_MAX 1024-
40#endif-
41-
42#define MAXSIZE (SIZE_MAX < SSIZE_MAX ? SIZE_MAX : SSIZE_MAX)-
43-
44/* Call readlink to get the symbolic link value of FILE.-
45 SIZE is a hint as to how long the link is expected to be;-
46 typically it is taken from st_size. It need not be correct.-
47 Return a pointer to that NUL-terminated string in malloc'd storage.-
48 If readlink fails, malloc fails, or if the link value is longer-
49 than SSIZE_MAX, return NULL (caller may use errno to diagnose). */-
50-
51char *-
52areadlink_with_size (char const *file, size_t size)-
53{-
54 /* Some buggy file systems report garbage in st_size. Defend-
55 against them by ignoring outlandish st_size values in the initial-
56 memory allocation. */-
57 size_t symlink_max = SYMLINK_MAX;-
58 size_t INITIAL_LIMIT_BOUND = 8 * 1024;-
59 size_t initial_limit = (symlink_max < INITIAL_LIMIT_BOUND
symlink_max < ...AL_LIMIT_BOUNDDescription
TRUEevaluated 227 times by 7 tests
Evaluated by:
  • cp
  • ln
  • ls
  • mv
  • readlink
  • realpath
  • vdir
FALSEnever evaluated
0-227
60 ? symlink_max + 1-
61 : INITIAL_LIMIT_BOUND);-
62-
63 /* The initial buffer size for the link value. */-
64 size_t buf_size = size < initial_limit ? size + 1 : initial_limit;
size < initial_limitDescription
TRUEevaluated 227 times by 7 tests
Evaluated by:
  • cp
  • ln
  • ls
  • mv
  • readlink
  • realpath
  • vdir
FALSEnever evaluated
0-227
65-
66 while (1)-
67 {-
68 ssize_t r;-
69 size_t link_length;-
70 char *buffer = malloc (buf_size);-
71-
72 if (buffer == NULL)
buffer == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 227 times by 7 tests
Evaluated by:
  • cp
  • ln
  • ls
  • mv
  • readlink
  • realpath
  • vdir
0-227
73 return NULL;
never executed: return ((void *)0) ;
0
74 r = readlink (file, buffer, buf_size);-
75 link_length = r;-
76-
77 /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1-
78 with errno == ERANGE if the buffer is too small. */-
79 if (r < 0 && errno != ERANGE)
r < 0Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • readlink
FALSEevaluated 222 times by 7 tests
Evaluated by:
  • cp
  • ln
  • ls
  • mv
  • readlink
  • realpath
  • vdir
(*__errno_location ()) != 34Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • readlink
FALSEnever evaluated
0-222
80 {-
81 int saved_errno = errno;-
82 free (buffer);-
83 errno = saved_errno;-
84 return NULL;
executed 5 times by 1 test: return ((void *)0) ;
Executed by:
  • readlink
5
85 }-
86-
87 if (link_length < buf_size)
link_length < buf_sizeDescription
TRUEevaluated 222 times by 7 tests
Evaluated by:
  • cp
  • ln
  • ls
  • mv
  • readlink
  • realpath
  • vdir
FALSEnever evaluated
0-222
88 {-
89 buffer[link_length] = 0;-
90 return buffer;
executed 222 times by 7 tests: return buffer;
Executed by:
  • cp
  • ln
  • ls
  • mv
  • readlink
  • realpath
  • vdir
222
91 }-
92-
93 free (buffer);-
94 if (buf_size <= MAXSIZE / 2)
buf_size <= ( ...ffffffffL) / 2Description
TRUEnever evaluated
FALSEnever evaluated
(1844674407370...fffffffffffffLDescription
TRUEnever evaluated
FALSEnever evaluated
0
95 buf_size *= 2;
never executed: buf_size *= 2;
0
96 else if (buf_size < MAXSIZE)
buf_size < ( (...ffffffffffffL)Description
TRUEnever evaluated
FALSEnever evaluated
0
97 buf_size = MAXSIZE;
never executed: buf_size = ( (18446744073709551615UL) < 0x7fffffffffffffffL ? (18446744073709551615UL) : 0x7fffffffffffffffL);
(1844674407370...fffffffffffffLDescription
TRUEnever evaluated
FALSEnever evaluated
0
98 else-
99 {-
100 errno = ENOMEM;-
101 return NULL;
never executed: return ((void *)0) ;
0
102 }-
103 }-
104}
never executed: end of block
0
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2