Skip to content

Commit

Permalink
Fix length calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
moriyoshi committed Apr 27, 2020
1 parent e88712b commit 7f2e3fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
26 changes: 10 additions & 16 deletions xoauth2_socket_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,24 @@ static int xoauth2_plugin_unix_socket_read(const sasl_utils_t *utils, xoauth2_pl
unsigned total_sz;
const xoauth2_plugin_socket_iovec_t *e;

/* Roll to INT_MAX */
if (nivs > (((uint32_t)-1) >> 1)) {
nivs = (((uint32_t)-1) >> 1);
/* Roll to (INT_MAX + 1) / 16 - 1 */
if (nivs > (((uint32_t)-1) >> 4)) {
nivs = (((uint32_t)-1) >> 4);
}

e = iv + nivs;
{
const xoauth2_plugin_socket_iovec_t *p;
total_sz = 0;
for (p = iv, e = iv + nivs; p < e; p++) {
if (total_sz + p->iov_len < total_sz) {
total_sz = (((uint32_t)-1) >> 1);
break;
}
if (total_sz > (((uint32_t)-1) >> 1)) {
total_sz = (((uint32_t)-1) >> 1);
break;
}
total_sz += p->iov_len;
}
e = p;
}

if (!minread) {
Expand All @@ -204,7 +202,7 @@ static int xoauth2_plugin_unix_socket_read(const sasl_utils_t *utils, xoauth2_pl
return SASL_FAIL;
}
}
n = readv(s->s, (struct iovec *)iv, nivs);
n = readv(s->s, (struct iovec *)iv, e - iv);
first = 0;
if (-1 == n) {
if (EWOULDBLOCK == errno) {
Expand All @@ -226,7 +224,6 @@ static int xoauth2_plugin_unix_socket_read(const sasl_utils_t *utils, xoauth2_pl
while (n > iv->iov_len) {
n -= iv->iov_len;
++iv;
--nivs;
}
*((unsigned char **)&iv->iov_base) += n;
iv->iov_len -= n;
Expand All @@ -240,26 +237,24 @@ static int xoauth2_plugin_unix_socket_write(const sasl_utils_t *utils, xoauth2_p
size_t total_sz;
const xoauth2_plugin_socket_iovec_t *e;

/* Roll to INT_MAX */
if (nivs > (((uint32_t)-1) >> 1)) {
nivs = (((uint32_t)-1) >> 1);
/* Roll to (INT_MAX + 1) / 16 - 1 */
if (nivs > (((uint32_t)-1) >> 4)) {
nivs = (((uint32_t)-1) >> 4);
}

e = iv + nivs;
{
const xoauth2_plugin_socket_iovec_t *p;
total_sz = 0;
for (p = iv, e = iv + nivs; p < e; p++) {
if (total_sz + p->iov_len < total_sz) {
total_sz = (((uint32_t)-1) >> 1);
break;
}
if (total_sz > (((uint32_t)-1) >> 1)) {
total_sz = (((uint32_t)-1) >> 1);
break;
}
total_sz += p->iov_len;
}
e = p;
}

*nwritten = 0;
Expand All @@ -280,7 +275,7 @@ static int xoauth2_plugin_unix_socket_write(const sasl_utils_t *utils, xoauth2_p
return SASL_FAIL;
}
}
n = writev(s->s, (struct iovec *)iv, nivs);
n = writev(s->s, (struct iovec *)iv, e - iv);
if (-1 == n) {
if (EWOULDBLOCK == errno) {
continue;
Expand All @@ -297,7 +292,6 @@ static int xoauth2_plugin_unix_socket_write(const sasl_utils_t *utils, xoauth2_p
while (n > iv->iov_len) {
n -= iv->iov_len;
++iv;
--nivs;
}
*((unsigned char **)&iv->iov_base) += n;
iv->iov_len -= n;
Expand Down
25 changes: 10 additions & 15 deletions xoauth2_socket_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ static int xoauth2_plugin_win32_socket_read(const sasl_utils_t *utils, xoauth2_p
WSABUF *wsiv, *e_wsiv;

/* Roll to (INT_MAX + 1) / 16 - 1 */
if (nivs > (((uint32_t)-1) >> 5)) {
nivs = (((uint32_t)-1) >> 5);
if (nivs > (((uint32_t)-1) >> 4)) {
nivs = (((uint32_t)-1) >> 4);
}

if (sizeof(WSABUF) * nivs < nivs) {
Expand All @@ -190,16 +190,14 @@ static int xoauth2_plugin_win32_socket_read(const sasl_utils_t *utils, xoauth2_p
const xoauth2_plugin_socket_iovec_t *p, *e = iv + nivs;
total_sz = 0;
for (p = iv, e = iv + nivs; p < e; p++) {
p_wsiv->len = p->iov_len;
p_wsiv->buf = p->iov_base;
if (total_sz + p->iov_len < total_sz) {
total_sz = (((uint32_t)-1) >> 1);
break;
}
if (total_sz > (((uint32_t)-1) >> 1)) {
total_sz = (((uint32_t)-1) >> 1);
break;
}
p_wsiv->len = p->iov_len;
p_wsiv->buf = p->iov_base;
total_sz += p->iov_len;
++p_wsiv;
}
Expand Down Expand Up @@ -259,7 +257,7 @@ static int xoauth2_plugin_win32_socket_read(const sasl_utils_t *utils, xoauth2_p
goto out;
}
}
wsc = WSARecv(s->s, wsiv, (DWORD)nivs, &n, &flags, NULL, NULL);
wsc = WSARecv(s->s, p_wsiv, (DWORD)(e_wsiv - p_wsiv), &n, &flags, NULL, NULL);
first = 0;
if (SOCKET_ERROR == wsc) {
int _errno = WSAGetLastError();
Expand All @@ -286,7 +284,6 @@ static int xoauth2_plugin_win32_socket_read(const sasl_utils_t *utils, xoauth2_p
while (n > p_wsiv->len) {
n -= p_wsiv->len;
++p_wsiv;
--nivs;
}
*((unsigned char **)&p_wsiv->buf) += n;
p_wsiv->len -= n;
Expand All @@ -306,8 +303,8 @@ static int xoauth2_plugin_win32_socket_write(const sasl_utils_t *utils, xoauth2_
WSABUF *wsiv, *e_wsiv;

/* Roll to (INT_MAX + 1) / 16 - 1 */
if (nivs > (((uint32_t)-1) >> 5)) {
nivs = (((uint32_t)-1) >> 5);
if (nivs > (((uint32_t)-1) >> 4)) {
nivs = (((uint32_t)-1) >> 4);
}

if (sizeof(WSABUF) * nivs < nivs) {
Expand All @@ -323,16 +320,14 @@ static int xoauth2_plugin_win32_socket_write(const sasl_utils_t *utils, xoauth2_
const xoauth2_plugin_socket_iovec_t *p, *e = iv + nivs;
total_sz = 0;
for (p = iv, e = iv + nivs; p < e; p++) {
p_wsiv->len = p->iov_len;
p_wsiv->buf = p->iov_base;
if (total_sz + p->iov_len < total_sz) {
total_sz = (((uint32_t)-1) >> 1);
break;
}
if (total_sz > (((uint32_t)-1) >> 1)) {
total_sz = (((uint32_t)-1) >> 1);
break;
}
p_wsiv->len = p->iov_len;
p_wsiv->buf = p->iov_base;
total_sz += p->iov_len;
++p_wsiv;
}
Expand Down Expand Up @@ -384,7 +379,7 @@ static int xoauth2_plugin_win32_socket_write(const sasl_utils_t *utils, xoauth2_
goto out;
}
}
if (SOCKET_ERROR == WSASend(s->s, p_wsiv, (DWORD)nivs, &n, 0, NULL, NULL)) {
if (SOCKET_ERROR == WSASend(s->s, p_wsiv, (DWORD)(e_wsiv - p_wsiv), &n, 0, NULL, NULL)) {
int _errno = WSAGetLastError();
if (WSAEWOULDBLOCK == _errno) {
continue;
Expand Down

0 comments on commit 7f2e3fb

Please sign in to comment.