Skip to content

Commit

Permalink
lib: add void *ctx to reader/writer instances
Browse files Browse the repository at this point in the history
- `struct Curl_cwriter` and `struct Curl_creader` now carry a
  `void *ctx` member that points to the instance as allocated.
- using `r->ctx` and `w->ctx` as pointer to the instance specific
  struct that has been allocated

Reported-by: Rudi Heitbaum
Fixes curl#13035
Closes curl#13059
  • Loading branch information
icing authored and bagder committed Mar 6, 2024
1 parent 2ca530d commit 9978d40
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 50 deletions.
6 changes: 3 additions & 3 deletions lib/cw-out.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ struct Curl_cwtype Curl_cwt_out = {
static CURLcode cw_out_init(struct Curl_easy *data,
struct Curl_cwriter *writer)
{
struct cw_out_ctx *ctx = (struct cw_out_ctx *)writer;
struct cw_out_ctx *ctx = writer->ctx;
(void)data;
ctx->buf = NULL;
return CURLE_OK;
Expand Down Expand Up @@ -151,7 +151,7 @@ static size_t cw_out_bufs_len(struct cw_out_ctx *ctx)

static void cw_out_close(struct Curl_easy *data, struct Curl_cwriter *writer)
{
struct cw_out_ctx *ctx = (struct cw_out_ctx *)writer;
struct cw_out_ctx *ctx = writer->ctx;

(void)data;
cw_out_bufs_free(ctx);
Expand Down Expand Up @@ -378,7 +378,7 @@ static CURLcode cw_out_write(struct Curl_easy *data,
struct Curl_cwriter *writer, int type,
const char *buf, size_t blen)
{
struct cw_out_ctx *ctx = (struct cw_out_ctx *)writer;
struct cw_out_ctx *ctx = writer->ctx;
CURLcode result;
bool flush_all;

Expand Down
2 changes: 1 addition & 1 deletion lib/ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ static CURLcode ftp_cw_lc_write(struct Curl_easy *data,
const char *buf, size_t blen)
{
static const char nl = '\n';
struct ftp_cw_lc_ctx *ctx = (struct ftp_cw_lc_ctx *)writer;
struct ftp_cw_lc_ctx *ctx = writer->ctx;

if(!(type & CLIENTWRITE_BODY) ||
data->conn->proto.ftpc.transfertype != 'A')
Expand Down
16 changes: 8 additions & 8 deletions lib/http_chunks.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ struct chunked_writer {
static CURLcode cw_chunked_init(struct Curl_easy *data,
struct Curl_cwriter *writer)
{
struct chunked_writer *ctx = (struct chunked_writer *)writer;
struct chunked_writer *ctx = writer->ctx;

data->req.chunk = TRUE; /* chunks coming our way. */
Curl_httpchunk_init(data, &ctx->ch, FALSE);
Expand All @@ -404,15 +404,15 @@ static CURLcode cw_chunked_init(struct Curl_easy *data,
static void cw_chunked_close(struct Curl_easy *data,
struct Curl_cwriter *writer)
{
struct chunked_writer *ctx = (struct chunked_writer *)writer;
struct chunked_writer *ctx = writer->ctx;
Curl_httpchunk_free(data, &ctx->ch);
}

static CURLcode cw_chunked_write(struct Curl_easy *data,
struct Curl_cwriter *writer, int type,
const char *buf, size_t blen)
{
struct chunked_writer *ctx = (struct chunked_writer *)writer;
struct chunked_writer *ctx = writer->ctx;
CURLcode result;
size_t consumed;

Expand Down Expand Up @@ -474,7 +474,7 @@ struct chunked_reader {
static CURLcode cr_chunked_init(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct chunked_reader *ctx = (struct chunked_reader *)reader;
struct chunked_reader *ctx = reader->ctx;
(void)data;
Curl_bufq_init2(&ctx->chunkbuf, CURL_CHUNKED_MAXLEN, 2, BUFQ_OPT_SOFT_LIMIT);
return CURLE_OK;
Expand All @@ -483,15 +483,15 @@ static CURLcode cr_chunked_init(struct Curl_easy *data,
static void cr_chunked_close(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct chunked_reader *ctx = (struct chunked_reader *)reader;
struct chunked_reader *ctx = reader->ctx;
(void)data;
Curl_bufq_free(&ctx->chunkbuf);
}

static CURLcode add_last_chunk(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct chunked_reader *ctx = (struct chunked_reader *)reader;
struct chunked_reader *ctx = reader->ctx;
struct curl_slist *trailers = NULL, *tr;
CURLcode result;
size_t n;
Expand Down Expand Up @@ -542,7 +542,7 @@ static CURLcode add_chunk(struct Curl_easy *data,
struct Curl_creader *reader,
char *buf, size_t blen)
{
struct chunked_reader *ctx = (struct chunked_reader *)reader;
struct chunked_reader *ctx = reader->ctx;
CURLcode result;
char tmp[CURL_CHUNKED_MINLEN];
size_t nread;
Expand Down Expand Up @@ -595,7 +595,7 @@ static CURLcode cr_chunked_read(struct Curl_easy *data,
char *buf, size_t blen,
size_t *pnread, bool *peos)
{
struct chunked_reader *ctx = (struct chunked_reader *)reader;
struct chunked_reader *ctx = reader->ctx;
CURLcode result = CURLE_READ_ERROR;

*pnread = 0;
Expand Down
16 changes: 8 additions & 8 deletions lib/mime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1928,7 +1928,7 @@ struct cr_mime_ctx {
static CURLcode cr_mime_init(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct cr_mime_ctx *ctx = (struct cr_mime_ctx *)reader;
struct cr_mime_ctx *ctx = reader->ctx;
(void)data;
ctx->total_len = -1;
ctx->read_len = 0;
Expand All @@ -1941,7 +1941,7 @@ static CURLcode cr_mime_read(struct Curl_easy *data,
char *buf, size_t blen,
size_t *pnread, bool *peos)
{
struct cr_mime_ctx *ctx = (struct cr_mime_ctx *)reader;
struct cr_mime_ctx *ctx = reader->ctx;
size_t nread;

/* Once we have errored, we will return the same error forever */
Expand Down Expand Up @@ -2022,15 +2022,15 @@ static CURLcode cr_mime_read(struct Curl_easy *data,
static bool cr_mime_needs_rewind(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct cr_mime_ctx *ctx = (struct cr_mime_ctx *)reader;
struct cr_mime_ctx *ctx = reader->ctx;
(void)data;
return ctx->read_len > 0;
}

static curl_off_t cr_mime_total_length(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct cr_mime_ctx *ctx = (struct cr_mime_ctx *)reader;
struct cr_mime_ctx *ctx = reader->ctx;
(void)data;
return ctx->total_len;
}
Expand All @@ -2039,7 +2039,7 @@ static CURLcode cr_mime_resume_from(struct Curl_easy *data,
struct Curl_creader *reader,
curl_off_t offset)
{
struct cr_mime_ctx *ctx = (struct cr_mime_ctx *)reader;
struct cr_mime_ctx *ctx = reader->ctx;

if(offset > 0) {
curl_off_t passed = 0;
Expand Down Expand Up @@ -2080,7 +2080,7 @@ static CURLcode cr_mime_resume_from(struct Curl_easy *data,
static CURLcode cr_mime_rewind(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct cr_mime_ctx *ctx = (struct cr_mime_ctx *)reader;
struct cr_mime_ctx *ctx = reader->ctx;
CURLcode result = mime_rewind(ctx->part);
if(result)
failf(data, "Cannot rewind mime/post data");
Expand All @@ -2090,7 +2090,7 @@ static CURLcode cr_mime_rewind(struct Curl_easy *data,
static CURLcode cr_mime_unpause(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct cr_mime_ctx *ctx = (struct cr_mime_ctx *)reader;
struct cr_mime_ctx *ctx = reader->ctx;
(void)data;
mime_unpause(ctx->part);
return CURLE_OK;
Expand Down Expand Up @@ -2118,7 +2118,7 @@ CURLcode Curl_creader_set_mime(struct Curl_easy *data, curl_mimepart *part)
result = Curl_creader_create(&r, data, &cr_mime, CURL_CR_CLIENT);
if(result)
return result;
ctx = (struct cr_mime_ctx *)r;
ctx = r->ctx;
ctx->part = part;
/* Make sure we will read the entire mime structure. */
result = mime_rewind(ctx->part);
Expand Down
50 changes: 28 additions & 22 deletions lib/sendf.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ static CURLcode cw_download_write(struct Curl_easy *data,
struct Curl_cwriter *writer, int type,
const char *buf, size_t nbytes)
{
struct cw_download_ctx *ctx = (struct cw_download_ctx *)writer;
struct cw_download_ctx *ctx = writer->ctx;
CURLcode result;
size_t nwrite, excess_len = 0;
bool is_connect = !!(type & CLIENTWRITE_CONNECT);
Expand Down Expand Up @@ -368,15 +368,18 @@ CURLcode Curl_cwriter_create(struct Curl_cwriter **pwriter,
const struct Curl_cwtype *cwt,
Curl_cwriter_phase phase)
{
struct Curl_cwriter *writer;
struct Curl_cwriter *writer = NULL;
CURLcode result = CURLE_OUT_OF_MEMORY;
void *p;

DEBUGASSERT(cwt->cwriter_size >= sizeof(struct Curl_cwriter));
writer = (struct Curl_cwriter *) calloc(1, cwt->cwriter_size);
if(!writer)
p = calloc(1, cwt->cwriter_size);
if(!p)
goto out;

writer = (struct Curl_cwriter *)p;
writer->cwt = cwt;
writer->ctx = p;
writer->phase = phase;
result = cwt->do_init(data, writer);

Expand Down Expand Up @@ -575,7 +578,7 @@ struct cr_in_ctx {

static CURLcode cr_in_init(struct Curl_easy *data, struct Curl_creader *reader)
{
struct cr_in_ctx *ctx = (struct cr_in_ctx *)reader;
struct cr_in_ctx *ctx = reader->ctx;
(void)data;
ctx->read_cb = data->state.fread_func;
ctx->cb_user_data = data->state.in;
Expand All @@ -590,7 +593,7 @@ static CURLcode cr_in_read(struct Curl_easy *data,
char *buf, size_t blen,
size_t *pnread, bool *peos)
{
struct cr_in_ctx *ctx = (struct cr_in_ctx *)reader;
struct cr_in_ctx *ctx = reader->ctx;
size_t nread;

/* Once we have errored, we will return the same error forever */
Expand Down Expand Up @@ -681,15 +684,15 @@ static CURLcode cr_in_read(struct Curl_easy *data,
static bool cr_in_needs_rewind(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct cr_in_ctx *ctx = (struct cr_in_ctx *)reader;
struct cr_in_ctx *ctx = reader->ctx;
(void)data;
return ctx->has_used_cb;
}

static curl_off_t cr_in_total_length(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct cr_in_ctx *ctx = (struct cr_in_ctx *)reader;
struct cr_in_ctx *ctx = reader->ctx;
(void)data;
return ctx->total_len;
}
Expand All @@ -698,7 +701,7 @@ static CURLcode cr_in_resume_from(struct Curl_easy *data,
struct Curl_creader *reader,
curl_off_t offset)
{
struct cr_in_ctx *ctx = (struct cr_in_ctx *)reader;
struct cr_in_ctx *ctx = reader->ctx;
int seekerr = CURL_SEEKFUNC_CANTSEEK;

DEBUGASSERT(data->conn);
Expand Down Expand Up @@ -760,7 +763,7 @@ static CURLcode cr_in_resume_from(struct Curl_easy *data,
static CURLcode cr_in_rewind(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct cr_in_ctx *ctx = (struct cr_in_ctx *)reader;
struct cr_in_ctx *ctx = reader->ctx;

/* If we never invoked the callback, there is noting to rewind */
if(!ctx->has_used_cb)
Expand Down Expand Up @@ -830,15 +833,18 @@ CURLcode Curl_creader_create(struct Curl_creader **preader,
const struct Curl_crtype *crt,
Curl_creader_phase phase)
{
struct Curl_creader *reader;
struct Curl_creader *reader = NULL;
CURLcode result = CURLE_OUT_OF_MEMORY;
void *p;

DEBUGASSERT(crt->creader_size >= sizeof(struct Curl_creader));
reader = (struct Curl_creader *) calloc(1, crt->creader_size);
if(!reader)
p = calloc(1, crt->creader_size);
if(!p)
goto out;

reader = (struct Curl_creader *)p;
reader->crt = crt;
reader->ctx = p;
reader->phase = phase;
result = crt->do_init(data, reader);

Expand Down Expand Up @@ -866,15 +872,15 @@ struct cr_lc_ctx {

static CURLcode cr_lc_init(struct Curl_easy *data, struct Curl_creader *reader)
{
struct cr_lc_ctx *ctx = (struct cr_lc_ctx *)reader;
struct cr_lc_ctx *ctx = reader->ctx;
(void)data;
Curl_bufq_init2(&ctx->buf, (16 * 1024), 1, BUFQ_OPT_SOFT_LIMIT);
return CURLE_OK;
}

static void cr_lc_close(struct Curl_easy *data, struct Curl_creader *reader)
{
struct cr_lc_ctx *ctx = (struct cr_lc_ctx *)reader;
struct cr_lc_ctx *ctx = reader->ctx;
(void)data;
Curl_bufq_free(&ctx->buf);
}
Expand All @@ -885,7 +891,7 @@ static CURLcode cr_lc_read(struct Curl_easy *data,
char *buf, size_t blen,
size_t *pnread, bool *peos)
{
struct cr_lc_ctx *ctx = (struct cr_lc_ctx *)reader;
struct cr_lc_ctx *ctx = reader->ctx;
CURLcode result;
size_t nread, i, start, n;
bool eos;
Expand Down Expand Up @@ -1024,7 +1030,7 @@ CURLcode Curl_creader_set_fread(struct Curl_easy *data, curl_off_t len)
result = Curl_creader_create(&r, data, &cr_in, CURL_CR_CLIENT);
if(result)
return result;
ctx = (struct cr_in_ctx *)r;
ctx = r->ctx;
ctx->total_len = len;

cl_reset_reader(data);
Expand Down Expand Up @@ -1161,7 +1167,7 @@ static CURLcode cr_buf_read(struct Curl_easy *data,
char *buf, size_t blen,
size_t *pnread, bool *peos)
{
struct cr_buf_ctx *ctx = (struct cr_buf_ctx *)reader;
struct cr_buf_ctx *ctx = reader->ctx;
size_t nread = ctx->blen - ctx->index;

(void)data;
Expand All @@ -1183,15 +1189,15 @@ static CURLcode cr_buf_read(struct Curl_easy *data,
static bool cr_buf_needs_rewind(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct cr_buf_ctx *ctx = (struct cr_buf_ctx *)reader;
struct cr_buf_ctx *ctx = reader->ctx;
(void)data;
return ctx->index > 0;
}

static curl_off_t cr_buf_total_length(struct Curl_easy *data,
struct Curl_creader *reader)
{
struct cr_buf_ctx *ctx = (struct cr_buf_ctx *)reader;
struct cr_buf_ctx *ctx = reader->ctx;
(void)data;
return (curl_off_t)ctx->blen;
}
Expand All @@ -1200,7 +1206,7 @@ static CURLcode cr_buf_resume_from(struct Curl_easy *data,
struct Curl_creader *reader,
curl_off_t offset)
{
struct cr_buf_ctx *ctx = (struct cr_buf_ctx *)reader;
struct cr_buf_ctx *ctx = reader->ctx;
size_t boffset;

(void)data;
Expand Down Expand Up @@ -1242,7 +1248,7 @@ CURLcode Curl_creader_set_buf(struct Curl_easy *data,
result = Curl_creader_create(&r, data, &cr_buf, CURL_CR_CLIENT);
if(result)
return result;
ctx = (struct cr_buf_ctx *)r;
ctx = r->ctx;
ctx->buf = buf;
ctx->blen = blen;
ctx->index = 0;
Expand Down
Loading

0 comments on commit 9978d40

Please sign in to comment.