Skip to content

Commit 6b74ae5

Browse files
committed
bio: simplify BIO_gets null-termination, improve ossl compat
1 parent 5107613 commit 6b74ae5

1 file changed

Lines changed: 18 additions & 20 deletions

File tree

src/bio.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -600,15 +600,21 @@ static int wolfSSL_BIO_MEMORY_write(WOLFSSL_BIO* bio, const void* data,
600600
{
601601
WOLFSSL_ENTER("wolfSSL_BIO_MEMORY_write");
602602

603-
if (bio == NULL || bio->mem_buf == NULL || data == NULL) {
603+
/* ossl returns 0 on bio == NULL */
604+
if (bio == NULL) {
605+
return 0;
606+
}
607+
608+
if (bio->mem_buf == NULL || data == NULL) {
604609
WOLFSSL_MSG("one of input parameters is null");
605610
return WOLFSSL_BIO_ERROR;
606611
}
612+
607613
if (bio->flags & WOLFSSL_BIO_FLAG_MEM_RDONLY) {
608614
return WOLFSSL_BIO_ERROR;
609615
}
610616

611-
if (len == 0)
617+
if (len <= 0)
612618
return 0; /* Nothing to write */
613619

614620
if (wolfSSL_BUF_MEM_grow_ex(bio->mem_buf, ((size_t)bio->wrSz) +
@@ -1108,20 +1114,17 @@ int wolfSSL_BIO_gets(WOLFSSL_BIO* bio, char* buf, int sz)
11081114
}
11091115

11101116
cSz = wolfSSL_getLineLength((char*)c, cSz);
1111-
/* check case where line was bigger then buffer and buffer
1112-
* needs end terminator */
11131117
if (cSz >= sz) {
11141118
cSz = sz - 1;
1115-
buf[cSz] = '\0';
1116-
}
1117-
else {
1118-
/* not minus 1 here because placing terminator after
1119-
msg and have checked that sz is large enough */
1120-
buf[cSz] = '\0';
11211119
}
11221120

11231121
ret = wolfSSL_BIO_MEMORY_read(bio, (void*)buf, cSz);
1124-
/* ret is read after the switch statement */
1122+
if (ret > 0) {
1123+
buf[ret] = '\0';
1124+
}
1125+
else {
1126+
buf[0] = '\0';
1127+
}
11251128
break;
11261129
}
11271130
case WOLFSSL_BIO_BIO:
@@ -1136,23 +1139,18 @@ int wolfSSL_BIO_gets(WOLFSSL_BIO* bio, char* buf, int sz)
11361139
}
11371140

11381141
cSz = wolfSSL_getLineLength(c, cSz);
1139-
/* check case where line was bigger then buffer and buffer
1140-
* needs end terminator */
11411142
if (cSz >= sz) {
11421143
cSz = sz - 1;
1143-
buf[cSz] = '\0';
1144-
}
1145-
else {
1146-
/* not minus 1 here because placing terminator after
1147-
msg and have checked that sz is large enough */
1148-
buf[cSz] = '\0';
11491144
}
11501145

11511146
ret = wolfSSL_BIO_nread(bio, &c, cSz);
1152-
if (ret > 0 && ret < sz) {
1147+
if (ret > 0) {
11531148
XMEMCPY(buf, c, (size_t)ret);
11541149
buf[ret] = '\0';
11551150
}
1151+
else {
1152+
buf[0] = '\0';
1153+
}
11561154
break;
11571155
}
11581156

0 commit comments

Comments
 (0)