Skip to content

Commit f0b4afe

Browse files
Revert "Rework memory BIOs and implement BIO_seek (#2380)" (#2432)
This reverts commit 5a74835. Our Ruby CI checks with 3.4-3.2 started failing with this commit. This reverts the commit until we know what's up. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.
1 parent 4e97131 commit f0b4afe

File tree

2 files changed

+57
-176
lines changed

2 files changed

+57
-176
lines changed

crypto/bio/bio_mem.c

Lines changed: 24 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,11 @@
6565

6666
#include "../internal.h"
6767

68-
typedef struct bio_buf_mem_st {
69-
struct buf_mem_st *buf; /* allocated buffer */
70-
size_t read_off; /* read pointer offset from current buffer position */
71-
} BIO_BUF_MEM;
72-
7368

7469
BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len) {
7570
BIO *ret;
7671
BUF_MEM *b;
77-
BIO_BUF_MEM *bbm;
78-
79-
const size_t size = (len < 0 || (size_t)len > SIZE_MAX) ? strlen((char *)buf) : (size_t)len;
72+
const size_t size = len < 0 ? strlen((char *)buf) : (size_t)len;
8073

8174
if (!buf && len != 0) {
8275
OPENSSL_PUT_ERROR(BIO, BIO_R_NULL_PARAMETER);
@@ -88,8 +81,7 @@ BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len) {
8881
return NULL;
8982
}
9083

91-
bbm = (BIO_BUF_MEM *)ret->ptr;
92-
b = bbm->buf;
84+
b = (BUF_MEM *)ret->ptr;
9385
// BIO_FLAGS_MEM_RDONLY ensures |b->data| is not written to.
9486
b->data = (void *)buf;
9587
b->length = size;
@@ -106,25 +98,19 @@ BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len) {
10698
}
10799

108100
static int mem_new(BIO *bio) {
109-
BIO_BUF_MEM *bbm = OPENSSL_zalloc(sizeof(*bbm));
110-
111-
if (bbm == NULL) {
112-
return 0;
113-
}
101+
BUF_MEM *b;
114102

115-
bbm->buf = BUF_MEM_new();
116-
if (bbm->buf == NULL) {
117-
OPENSSL_free(bbm);
103+
b = BUF_MEM_new();
104+
if (b == NULL) {
118105
return 0;
119106
}
120107

121108
// |shutdown| is used to store the close flag: whether the BIO has ownership
122109
// of the BUF_MEM.
123-
bbm->read_off = 0;
124110
bio->shutdown = 1;
125111
bio->init = 1;
126112
bio->num = -1;
127-
bio->ptr = (char *)bbm;
113+
bio->ptr = (char *)b;
128114

129115
return 1;
130116
}
@@ -134,53 +120,35 @@ static int mem_free(BIO *bio) {
134120
return 1;
135121
}
136122

137-
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bio->ptr;
138-
BUF_MEM *b = bbm->buf;
139-
123+
BUF_MEM *b = (BUF_MEM *)bio->ptr;
140124
if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
141125
b->data = NULL;
142126
}
143127
BUF_MEM_free(b);
144128
bio->ptr = NULL;
145-
146-
OPENSSL_free(bbm);
147129
return 1;
148130
}
149131

150-
static void mem_buf_sync(BIO *bio) {
151-
if (bio->init != 0 && bio->ptr != NULL) {
152-
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *) bio->ptr;
153-
BUF_MEM *b = bbm->buf;
154-
155-
if (b->data != NULL) {
156-
if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
157-
b->data += bbm->read_off;
158-
} else {
159-
OPENSSL_memmove(b->data, &b->data[bbm->read_off], b->length);
160-
}
161-
bbm->read_off = 0;
162-
}
163-
}
164-
}
165-
166132
static int mem_read(BIO *bio, char *out, int outl) {
167133
BIO_clear_retry_flags(bio);
168134
if (outl <= 0) {
169135
return 0;
170136
}
171137

172-
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *) bio->ptr;
173-
BUF_MEM *b = bbm->buf;
174-
138+
BUF_MEM *b = bio->ptr;
175139
int ret = outl;
176140
if ((size_t)ret > b->length) {
177141
ret = (int)b->length;
178142
}
179143

180144
if (ret > 0) {
181-
OPENSSL_memcpy(out, &b->data[bbm->read_off], ret);
145+
OPENSSL_memcpy(out, b->data, ret);
182146
b->length -= ret;
183-
bbm->read_off += ret;
147+
if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
148+
b->data += ret;
149+
} else {
150+
OPENSSL_memmove(b->data, &b->data[ret], b->length);
151+
}
184152
} else if (b->length == 0) {
185153
ret = bio->num;
186154
if (ret != 0) {
@@ -201,11 +169,7 @@ static int mem_write(BIO *bio, const char *in, int inl) {
201169
return -1;
202170
}
203171

204-
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *) bio->ptr;
205-
BUF_MEM *b = bbm->buf;
206-
207-
mem_buf_sync(bio);
208-
172+
BUF_MEM *b = bio->ptr;
209173
if (!BUF_MEM_append(b, in, inl)) {
210174
return -1;
211175
}
@@ -221,21 +185,16 @@ static int mem_gets(BIO *bio, char *buf, int size) {
221185

222186
// The buffer size includes space for the trailing NUL, so we can read at most
223187
// one fewer byte.
224-
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *) bio->ptr;
225-
BUF_MEM *b = bbm->buf;
188+
BUF_MEM *b = bio->ptr;
226189
int ret = size - 1;
227190
if ((size_t)ret > b->length) {
228191
ret = (int)b->length;
229192
}
230193

231194
// Stop at the first newline.
232-
if (b->data != NULL) {
233-
char *readp = &b->data[bbm->read_off];
234-
235-
const char *newline = OPENSSL_memchr(readp, '\n', ret);
236-
if (newline != NULL) {
237-
ret = (int)(newline - readp + 1);
238-
}
195+
const char *newline = OPENSSL_memchr(b->data, '\n', ret);
196+
if (newline != NULL) {
197+
ret = (int)(newline - b->data + 1);
239198
}
240199

241200
ret = mem_read(bio, buf, ret);
@@ -248,8 +207,7 @@ static int mem_gets(BIO *bio, char *buf, int size) {
248207
static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
249208
long ret = 1;
250209

251-
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *) bio->ptr;
252-
BUF_MEM *b = bbm->buf;
210+
BUF_MEM *b = (BUF_MEM *)bio->ptr;
253211

254212
switch (cmd) {
255213
case BIO_CTRL_RESET:
@@ -262,31 +220,7 @@ static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
262220
OPENSSL_cleanse(b->data, b->max);
263221
b->length = 0;
264222
}
265-
bbm->read_off = 0;
266-
} else {
267-
ret = -1;
268-
}
269-
break;
270-
case BIO_C_FILE_SEEK:
271-
if (b->data == NULL || num < 0 || (size_t)num > b->max) {
272-
ret = -1;
273-
break;
274-
}
275-
276-
if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
277-
b->data -= b->max - b->length;
278-
b->length = b->max - num;
279-
} else {
280-
if ((size_t)num > bbm->read_off + b->length) {
281-
ret = -1;
282-
break;
283-
}
284-
285-
b->length = (b->length + bbm->read_off) - num;
286223
}
287-
288-
bbm->read_off = num;
289-
ret = num;
290224
break;
291225
case BIO_CTRL_EOF:
292226
ret = (long)(b->length == 0);
@@ -298,7 +232,7 @@ static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
298232
ret = (long)b->length;
299233
if (ptr != NULL) {
300234
char **pptr = ptr;
301-
*pptr = (b->data != NULL) ? &b->data[bbm->read_off] : NULL;
235+
*pptr = b->data;
302236
}
303237
break;
304238
case BIO_C_SET_BUF_MEM:
@@ -308,7 +242,6 @@ static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
308242
break;
309243
case BIO_C_GET_BUF_MEM_PTR:
310244
if (ptr != NULL) {
311-
mem_buf_sync(bio);
312245
BUF_MEM **pptr = ptr;
313246
*pptr = b;
314247
}
@@ -348,17 +281,14 @@ const BIO_METHOD *BIO_s_mem(void) { return &mem_method; }
348281

349282
int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents,
350283
size_t *out_len) {
284+
const BUF_MEM *b;
351285
if (!bio || bio->method != &mem_method) {
352286
return 0;
353287
}
354288

355-
BIO_BUF_MEM *bbm = (BIO_BUF_MEM *) bio->ptr;
356-
const BUF_MEM *b = bbm->buf;
357-
358-
mem_buf_sync((BIO *)bio);
359-
289+
b = (BUF_MEM *)bio->ptr;
360290
if (out_contents != NULL) {
361-
*out_contents = (b->data != NULL) ? (uint8_t *)&b->data[bbm->read_off] : NULL;
291+
*out_contents = (uint8_t *)b->data;
362292
}
363293
if(out_len) {
364294
*out_len = b->length;

0 commit comments

Comments
 (0)