Coverage Report

Created: 2022-02-19 20:28

/src/php-src/Zend/zend_stream.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 2.00 of the Zend license,     |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | http://www.zend.com/license/2_00.txt.                                |
11
   | If you did not receive a copy of the Zend license and are unable to  |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@zend.com so we can mail you a copy immediately.              |
14
   +----------------------------------------------------------------------+
15
   | Authors: Wez Furlong <wez@thebrainroom.com>                          |
16
   |          Scott MacVicar <scottmac@php.net>                           |
17
   |          Nuno Lopes <nlopess@php.net>                                |
18
   |          Marcus Boerger <helly@php.net>                              |
19
   +----------------------------------------------------------------------+
20
*/
21
22
#include "zend.h"
23
#include "zend_compile.h"
24
#include "zend_stream.h"
25
26
ZEND_DLIMPORT int isatty(int fd);
27
28
static ssize_t zend_stream_stdio_reader(void *handle, char *buf, size_t len) /* {{{ */
29
0
{
30
0
  return fread(buf, 1, len, (FILE*)handle);
31
0
} /* }}} */
32
33
static void zend_stream_stdio_closer(void *handle) /* {{{ */
34
0
{
35
0
  if (handle && (FILE*)handle != stdin) {
36
0
    fclose((FILE*)handle);
37
0
  }
38
0
} /* }}} */
39
40
static size_t zend_stream_stdio_fsizer(void *handle) /* {{{ */
41
0
{
42
0
  zend_stat_t buf;
43
0
  if (handle && zend_fstat(fileno((FILE*)handle), &buf) == 0) {
44
0
#ifdef S_ISREG
45
0
    if (!S_ISREG(buf.st_mode)) {
46
0
      return 0;
47
0
    }
48
0
#endif
49
0
    return buf.st_size;
50
0
  }
51
0
  return -1;
52
0
} /* }}} */
53
54
static size_t zend_stream_fsize(zend_file_handle *file_handle) /* {{{ */
55
958
{
56
958
  ZEND_ASSERT(file_handle->type == ZEND_HANDLE_STREAM);
57
958
  if (file_handle->handle.stream.isatty) {
58
0
    return 0;
59
0
  }
60
958
  return file_handle->handle.stream.fsizer(file_handle->handle.stream.handle);
61
958
} /* }}} */
62
63
0
ZEND_API void zend_stream_init_fp(zend_file_handle *handle, FILE *fp, const char *filename) {
64
0
  memset(handle, 0, sizeof(zend_file_handle));
65
0
  handle->type = ZEND_HANDLE_FP;
66
0
  handle->handle.fp = fp;
67
0
  handle->filename = filename;
68
0
}
69
70
378k
ZEND_API void zend_stream_init_filename(zend_file_handle *handle, const char *filename) {
71
378k
  memset(handle, 0, sizeof(zend_file_handle));
72
378k
  handle->type = ZEND_HANDLE_FILENAME;
73
378k
  handle->filename = filename;
74
378k
}
75
76
ZEND_API zend_result zend_stream_open(const char *filename, zend_file_handle *handle) /* {{{ */
77
17.4k
{
78
17.4k
  zend_string *opened_path;
79
17.4k
  if (zend_stream_open_function) {
80
17.4k
    return zend_stream_open_function(filename, handle);
81
17.4k
  }
82
83
0
  zend_stream_init_fp(handle, zend_fopen(filename, &opened_path), filename);
84
0
  handle->opened_path = opened_path;
85
0
  return handle->handle.fp ? SUCCESS : FAILURE;
86
0
} /* }}} */
87
88
static int zend_stream_getc(zend_file_handle *file_handle) /* {{{ */
89
0
{
90
0
  char buf;
91
92
0
  if (file_handle->handle.stream.reader(file_handle->handle.stream.handle, &buf, sizeof(buf))) {
93
0
    return (int)buf;
94
0
  }
95
0
  return EOF;
96
0
} /* }}} */
97
98
static ssize_t zend_stream_read(zend_file_handle *file_handle, char *buf, size_t len) /* {{{ */
99
13.9k
{
100
13.9k
  if (file_handle->handle.stream.isatty) {
101
0
    int c = '*';
102
0
    size_t n;
103
104
0
    for (n = 0; n < len && (c = zend_stream_getc(file_handle)) != EOF && c != '\n'; ++n)  {
105
0
      buf[n] = (char)c;
106
0
    }
107
0
    if (c == '\n') {
108
0
      buf[n++] = (char)c;
109
0
    }
110
111
0
    return n;
112
0
  }
113
13.9k
  return file_handle->handle.stream.reader(file_handle->handle.stream.handle, buf, len);
114
13.9k
} /* }}} */
115
116
ZEND_API zend_result zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t *len) /* {{{ */
117
378k
{
118
378k
  size_t file_size;
119
120
378k
  if (file_handle->buf) {
121
362k
    *buf = file_handle->buf;
122
362k
    *len = file_handle->len;
123
362k
    return SUCCESS;
124
362k
  }
125
126
16.1k
  if (file_handle->type == ZEND_HANDLE_FILENAME) {
127
16.1k
    if (zend_stream_open(file_handle->filename, file_handle) == FAILURE) {
128
15.2k
      return FAILURE;
129
15.2k
    }
130
958
  }
131
132
958
  if (file_handle->type == ZEND_HANDLE_FP) {
133
0
    if (!file_handle->handle.fp) {
134
0
      return FAILURE;
135
0
    }
136
137
0
    file_handle->type = ZEND_HANDLE_STREAM;
138
0
    file_handle->handle.stream.handle = file_handle->handle.fp;
139
0
    file_handle->handle.stream.isatty = isatty(fileno((FILE *)file_handle->handle.stream.handle));
140
0
    file_handle->handle.stream.reader = (zend_stream_reader_t)zend_stream_stdio_reader;
141
0
    file_handle->handle.stream.closer = (zend_stream_closer_t)zend_stream_stdio_closer;
142
0
    file_handle->handle.stream.fsizer = (zend_stream_fsizer_t)zend_stream_stdio_fsizer;
143
0
  }
144
145
958
  file_size = zend_stream_fsize(file_handle);
146
958
  if (file_size == (size_t)-1) {
147
1
    return FAILURE;
148
1
  }
149
150
957
  if (file_size) {
151
482
    ssize_t read;
152
482
    size_t size = 0;
153
482
    *buf = safe_emalloc(1, file_size, ZEND_MMAP_AHEAD);
154
1.54k
    while ((read = zend_stream_read(file_handle, *buf + size, file_size - size)) > 0) {
155
1.06k
      size += read;
156
1.06k
    }
157
482
    if (read < 0) {
158
76
      efree(*buf);
159
76
      return FAILURE;
160
76
    }
161
406
    file_handle->buf = *buf;
162
406
    file_handle->len = size;
163
475
  } else {
164
475
    size_t size = 0, remain = 4*1024;
165
475
    ssize_t read;
166
475
    *buf = emalloc(remain);
167
168
12.4k
    while ((read = zend_stream_read(file_handle, *buf + size, remain)) > 0) {
169
11.9k
      size   += read;
170
11.9k
      remain -= read;
171
172
11.9k
      if (remain == 0) {
173
783
        *buf   = safe_erealloc(*buf, size, 2, 0);
174
783
        remain = size;
175
783
      }
176
11.9k
    }
177
475
    if (read < 0) {
178
59
      efree(*buf);
179
59
      return FAILURE;
180
59
    }
181
182
416
    file_handle->len = size;
183
416
    if (size && remain < ZEND_MMAP_AHEAD) {
184
0
      *buf = safe_erealloc(*buf, size, 1, ZEND_MMAP_AHEAD);
185
0
    }
186
416
    file_handle->buf = *buf;
187
416
  }
188
189
822
  if (file_handle->len == 0) {
190
90
    *buf = erealloc(*buf, ZEND_MMAP_AHEAD);
191
90
    file_handle->buf = *buf;
192
90
  }
193
194
822
  memset(file_handle->buf + file_handle->len, 0, ZEND_MMAP_AHEAD);
195
196
822
  *buf = file_handle->buf;
197
822
  *len = file_handle->len;
198
199
822
  return SUCCESS;
200
957
} /* }}} */
201
202
ZEND_API void zend_file_handle_dtor(zend_file_handle *fh) /* {{{ */
203
377k
{
204
377k
  switch (fh->type) {
205
0
    case ZEND_HANDLE_FP:
206
0
      fclose(fh->handle.fp);
207
0
      break;
208
771
    case ZEND_HANDLE_STREAM:
209
771
      if (fh->handle.stream.closer && fh->handle.stream.handle) {
210
771
        fh->handle.stream.closer(fh->handle.stream.handle);
211
771
      }
212
771
      fh->handle.stream.handle = NULL;
213
771
      break;
214
376k
    case ZEND_HANDLE_FILENAME:
215
      /* We're only supposed to get here when destructing the used_files hash,
216
       * which doesn't really contain open files, but references to their names/paths
217
       */
218
376k
      break;
219
377k
  }
220
377k
  if (fh->opened_path) {
221
0
    zend_string_release_ex(fh->opened_path, 0);
222
0
    fh->opened_path = NULL;
223
0
  }
224
377k
  if (fh->buf) {
225
361k
    efree(fh->buf);
226
361k
    fh->buf = NULL;
227
361k
  }
228
377k
  if (fh->free_filename && fh->filename) {
229
0
    efree((char*)fh->filename);
230
0
    fh->filename = NULL;
231
0
  }
232
377k
}
233
/* }}} */
234
235
/* return int to be compatible with Zend linked list API */
236
ZEND_API int zend_compare_file_handles(zend_file_handle *fh1, zend_file_handle *fh2) /* {{{ */
237
31.7k
{
238
31.7k
  if (fh1->type != fh2->type) {
239
771
    return 0;
240
771
  }
241
30.9k
  switch (fh1->type) {
242
30.2k
    case ZEND_HANDLE_FILENAME:
243
30.2k
      return strcmp(fh1->filename, fh2->filename) == 0;
244
0
    case ZEND_HANDLE_FP:
245
0
      return fh1->handle.fp == fh2->handle.fp;
246
771
    case ZEND_HANDLE_STREAM:
247
771
      return fh1->handle.stream.handle == fh2->handle.stream.handle;
248
0
    default:
249
0
      return 0;
250
0
  }
251
0
  return 0;
252
0
} /* }}} */