Skip to content

Commit

Permalink
krb5: use dynbuf
Browse files Browse the repository at this point in the history
Closes #13568
  • Loading branch information
bagder committed May 9, 2024
1 parent a95fd86 commit 0f4c439
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
56 changes: 31 additions & 25 deletions lib/krb5.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,34 +524,44 @@ static CURLcode read_data(struct Curl_easy *data, int sockindex,
return result;

if(len) {
/* only realloc if there was a length */
len = ntohl(len);
if(len > CURL_MAX_INPUT_LENGTH)
len = 0;
else
buf->data = Curl_saferealloc(buf->data, len);
return CURLE_TOO_LARGE;

Curl_dyn_reset(&buf->buf);
}
if(!len || !buf->data)
return CURLE_OUT_OF_MEMORY;
else
return CURLE_RECV_ERROR;

result = socket_read(data, sockindex, buf->data, len);
if(result)
return result;
nread = conn->mech->decode(conn->app_data, buf->data, len,
conn->data_prot, conn);
do {
char buffer[1024];
nread = CURLMIN(len, (int)sizeof(buffer));
result = socket_read(data, sockindex, buffer, nread);
if(result)
return result;
result = Curl_dyn_addn(&buf->buf, buffer, nread);
if(result)
return result;
len -= nread;
} while(len);
/* this decodes the dynbuf *in place* */
nread = conn->mech->decode(conn->app_data,
Curl_dyn_ptr(&buf->buf),
len, conn->data_prot, conn);
if(nread < 0)
return CURLE_RECV_ERROR;
buf->size = (size_t)nread;
Curl_dyn_setlen(&buf->buf, nread);
buf->index = 0;
return CURLE_OK;
}

static size_t
buffer_read(struct krb5buffer *buf, void *data, size_t len)
{
if(buf->size - buf->index < len)
len = buf->size - buf->index;
memcpy(data, (char *)buf->data + buf->index, len);
size_t size = Curl_dyn_len(&buf->buf);
if(size - buf->index < len)
len = size - buf->index;
memcpy(data, Curl_dyn_ptr(&buf->buf) + buf->index, len);
buf->index += len;
return len;
}
Expand Down Expand Up @@ -586,7 +596,7 @@ static ssize_t sec_recv(struct Curl_easy *data, int sockindex,
while(len > 0) {
if(read_data(data, sockindex, &conn->in_buffer))
return -1;
if(conn->in_buffer.size == 0) {
if(Curl_dyn_len(&conn->in_buffer.buf) == 0) {
if(bytes_read > 0)
conn->in_buffer.eof_flag = 1;
return bytes_read;
Expand Down Expand Up @@ -835,6 +845,7 @@ static CURLcode choose_mech(struct Curl_easy *data, struct connectdata *conn)
mech->name);
return CURLE_FAILED_INIT;
}
Curl_dyn_init(&conn->in_buffer.buf, CURL_MAX_INPUT_LENGTH);
}

infof(data, "Trying mechanism %s...", mech->name);
Expand Down Expand Up @@ -899,15 +910,10 @@ Curl_sec_end(struct connectdata *conn)
{
if(conn->mech && conn->mech->end)
conn->mech->end(conn->app_data);
free(conn->app_data);
conn->app_data = NULL;
if(conn->in_buffer.data) {
free(conn->in_buffer.data);
conn->in_buffer.data = NULL;
conn->in_buffer.size = 0;
conn->in_buffer.index = 0;
conn->in_buffer.eof_flag = 0;
}
Curl_safefree(conn->app_data);
Curl_dyn_free(&conn->in_buffer.buf);
conn->in_buffer.index = 0;
conn->in_buffer.eof_flag = 0;
conn->sec_complete = 0;
conn->data_prot = PROT_CLEAR;
conn->mech = NULL;
Expand Down
3 changes: 1 addition & 2 deletions lib/urldata.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ typedef CURLcode (*Curl_datastream)(struct Curl_easy *data,
#ifdef HAVE_GSSAPI
/* Types needed for krb5-ftp connections */
struct krb5buffer {
void *data;
size_t size;
struct dynbuf buf;
size_t index;
BIT(eof_flag);
};
Expand Down

0 comments on commit 0f4c439

Please sign in to comment.