Skip to content

Commit 59f7b24

Browse files
committed
Make bufchain_prefix return a ptrlen.
Now that all the call sites are expecting a size_t instead of an int length field, it's no longer particularly difficult to make it actually return the pointer,length pair in the form of a ptrlen. It would be nice to say that simplifies call sites because those ptrlens can all be passed straight along to other ptrlen-consuming functions. Actually almost none of the call sites are like that _yet_, but this makes it possible to move them in that direction in future (as part of my general aim to migrate ptrlen-wards as much as I can). But also it's just nicer to keep the pointer and length together in one variable, and not have to declare them both in advance with two extra lines of boilerplate.
1 parent 0cda34c commit 59f7b24

27 files changed

+128
-163
lines changed

logging.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,9 @@ static void logfopen_callback(void *vctx, int mode)
137137
*/
138138
assert(ctx->state != L_OPENING); /* make _sure_ it won't be requeued */
139139
while (bufchain_size(&ctx->queue)) {
140-
void *data;
141-
size_t len;
142-
bufchain_prefix(&ctx->queue, &data, &len);
143-
logwrite(ctx, data, len);
144-
bufchain_consume(&ctx->queue, len);
140+
ptrlen data = bufchain_prefix(&ctx->queue);
141+
logwrite(ctx, data.ptr, data.len);
142+
bufchain_consume(&ctx->queue, data.len);
145143
}
146144
logflush(ctx);
147145
}

misc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void bufchain_init(bufchain *ch);
105105
void bufchain_clear(bufchain *ch);
106106
size_t bufchain_size(bufchain *ch);
107107
void bufchain_add(bufchain *ch, const void *data, size_t len);
108-
void bufchain_prefix(bufchain *ch, void **data, size_t *len);
108+
ptrlen bufchain_prefix(bufchain *ch);
109109
void bufchain_consume(bufchain *ch, size_t len);
110110
void bufchain_fetch(bufchain *ch, void *data, size_t len);
111111
void bufchain_fetch_consume(bufchain *ch, void *data, size_t len);

noprint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ printer_job *printer_start_job(char *printer)
1616
return NULL;
1717
}
1818

19-
void printer_job_data(printer_job *pj, void *data, int len)
19+
void printer_job_data(printer_job *pj, void *data, size_t len)
2020
{
2121
}
2222

proxy.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
*/
2525
void proxy_activate (ProxySocket *p)
2626
{
27-
void *data;
28-
size_t len, output_before, output_after;
27+
size_t output_before, output_after;
2928

3029
p->state = PROXY_STATE_ACTIVE;
3130

@@ -42,16 +41,16 @@ void proxy_activate (ProxySocket *p)
4241

4342
/* send buffered OOB writes */
4443
while (bufchain_size(&p->pending_oob_output_data) > 0) {
45-
bufchain_prefix(&p->pending_oob_output_data, &data, &len);
46-
output_after += sk_write_oob(p->sub_socket, data, len);
47-
bufchain_consume(&p->pending_oob_output_data, len);
44+
ptrlen data = bufchain_prefix(&p->pending_oob_output_data);
45+
output_after += sk_write_oob(p->sub_socket, data.ptr, data.len);
46+
bufchain_consume(&p->pending_oob_output_data, data.len);
4847
}
4948

5049
/* send buffered normal writes */
5150
while (bufchain_size(&p->pending_output_data) > 0) {
52-
bufchain_prefix(&p->pending_output_data, &data, &len);
53-
output_after += sk_write(p->sub_socket, data, len);
54-
bufchain_consume(&p->pending_output_data, len);
51+
ptrlen data = bufchain_prefix(&p->pending_output_data);
52+
output_after += sk_write(p->sub_socket, data.ptr, data.len);
53+
bufchain_consume(&p->pending_output_data, data.len);
5554
}
5655

5756
/* if we managed to send any data, let the higher levels know. */
@@ -159,15 +158,13 @@ static void sk_proxy_set_frozen (Socket *s, bool is_frozen)
159158
* so we have to check each time.
160159
*/
161160
while (!ps->freeze && bufchain_size(&ps->pending_input_data) > 0) {
162-
void *data;
163161
char databuf[512];
164-
size_t len;
165-
bufchain_prefix(&ps->pending_input_data, &data, &len);
166-
if (len > lenof(databuf))
167-
len = lenof(databuf);
168-
memcpy(databuf, data, len);
169-
bufchain_consume(&ps->pending_input_data, len);
170-
plug_receive(ps->plug, 0, databuf, len);
162+
ptrlen data = bufchain_prefix(&ps->pending_input_data);
163+
if (data.len > lenof(databuf))
164+
data.len = lenof(databuf);
165+
memcpy(databuf, data.ptr, data.len);
166+
bufchain_consume(&ps->pending_input_data, data.len);
167+
plug_receive(ps->plug, 0, databuf, data.len);
171168
}
172169

173170
/* if we're still frozen, we'll have to wait for another

putty.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,7 @@ printer_enum *printer_start_enum(int *nprinters);
18411841
char *printer_get_name(printer_enum *, int);
18421842
void printer_finish_enum(printer_enum *);
18431843
printer_job *printer_start_job(char *printer);
1844-
void printer_job_data(printer_job *, void *, int);
1844+
void printer_job_data(printer_job *, const void *, size_t);
18451845
void printer_finish_job(printer_job *);
18461846

18471847
/*

rlogin.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,8 @@ static size_t rlogin_send(Backend *be, const char *buf, size_t len)
287287

288288
if (!rlogin->prompt) {
289289
while (bufchain_size(&bc) > 0) {
290-
void *data;
291-
size_t len;
292-
bufchain_prefix(&bc, &data, &len);
293-
rlogin->bufsize = sk_write(rlogin->s, data, len);
290+
ptrlen data = bufchain_prefix(&bc);
291+
rlogin->bufsize = sk_write(rlogin->s, data.ptr, data.len);
294292
bufchain_consume(&bc, len);
295293
}
296294
}

scpserver.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,17 +1077,13 @@ static void scp_sink_coroutine(ScpSink *scp)
10771077
if (scp->input_eof)
10781078
goto done;
10791079

1080-
void *vdata;
1081-
size_t len;
1082-
const char *cdata, *newline;
1083-
1084-
bufchain_prefix(&scp->data, &vdata, &len);
1085-
cdata = vdata;
1086-
newline = memchr(cdata, '\012', len);
1080+
ptrlen data = bufchain_prefix(&scp->data);
1081+
const char *cdata = data.ptr;
1082+
const char *newline = memchr(cdata, '\012', data.len);
10871083
if (newline)
1088-
len = (int)(newline+1 - cdata);
1089-
put_data(scp->command, cdata, len);
1090-
bufchain_consume(&scp->data, len);
1084+
data.len = (int)(newline+1 - cdata);
1085+
put_data(scp->command, cdata, data.len);
1086+
bufchain_consume(&scp->data, data.len);
10911087

10921088
if (newline)
10931089
break;
@@ -1179,8 +1175,7 @@ static void scp_sink_coroutine(ScpSink *scp)
11791175
sshfwd_write(scp->sc, "\0", 1);
11801176
scp->file_offset = 0;
11811177
while (scp->file_offset < scp->file_size) {
1182-
void *vdata;
1183-
size_t len;
1178+
ptrlen data;
11841179
uint64_t this_len, remaining;
11851180

11861181
crMaybeWaitUntilV(
@@ -1191,14 +1186,14 @@ static void scp_sink_coroutine(ScpSink *scp)
11911186
goto done;
11921187
}
11931188

1194-
bufchain_prefix(&scp->data, &vdata, &len);
1195-
this_len = len;
1189+
data = bufchain_prefix(&scp->data);
1190+
this_len = data.len;
11961191
remaining = scp->file_size - scp->file_offset;
11971192
if (this_len > remaining)
11981193
this_len = remaining;
11991194
sftpsrv_write(scp->sf, &scp->reply.srb,
12001195
scp->reply.handle, scp->file_offset,
1201-
make_ptrlen(vdata, this_len));
1196+
make_ptrlen(data.ptr, this_len));
12021197
if (scp->reply.err) {
12031198
scp->errmsg = dupprintf(
12041199
"'%.*s': unable to write to file: %s",

ssh.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,17 +348,16 @@ static void ssh_bpp_output_raw_data_callback(void *vctx)
348348
return;
349349

350350
while (bufchain_size(&ssh->out_raw) > 0) {
351-
void *data;
352-
size_t len, backlog;
351+
size_t backlog;
353352

354-
bufchain_prefix(&ssh->out_raw, &data, &len);
353+
ptrlen data = bufchain_prefix(&ssh->out_raw);
355354

356355
if (ssh->logctx)
357-
log_packet(ssh->logctx, PKT_OUTGOING, -1, NULL, data, len,
356+
log_packet(ssh->logctx, PKT_OUTGOING, -1, NULL, data.ptr, data.len,
358357
0, NULL, NULL, 0, NULL);
359-
backlog = sk_write(ssh->s, data, len);
358+
backlog = sk_write(ssh->s, data.ptr, data.len);
360359

361-
bufchain_consume(&ssh->out_raw, len);
360+
bufchain_consume(&ssh->out_raw, data.len);
362361

363362
if (backlog > SSH_MAX_BACKLOG) {
364363
ssh_throttle_all(ssh, true, backlog);

ssh1connection.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -780,13 +780,11 @@ static void ssh1_connection_got_user_input(PacketProtocolLayer *ppl)
780780
/*
781781
* Add user input to the main channel's buffer.
782782
*/
783-
void *data;
784-
size_t len;
785-
bufchain_prefix(s->ppl.user_input, &data, &len);
786-
if (len > 512)
787-
len = 512;
788-
sshfwd_write(&s->mainchan_sc, data, len);
789-
bufchain_consume(s->ppl.user_input, len);
783+
ptrlen data = bufchain_prefix(s->ppl.user_input);
784+
if (data.len > 512)
785+
data.len = 512;
786+
sshfwd_write(&s->mainchan_sc, data.ptr, data.len);
787+
bufchain_consume(s->ppl.user_input, data.len);
790788
}
791789
}
792790

ssh2connection.c

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,16 +1064,14 @@ static size_t ssh2_try_send(struct ssh2_channel *c)
10641064
while (c->remwindow > 0 &&
10651065
(bufchain_size(&c->outbuffer) > 0 ||
10661066
bufchain_size(&c->errbuffer) > 0)) {
1067-
size_t len;
1068-
void *data;
10691067
bufchain *buf = (bufchain_size(&c->errbuffer) > 0 ?
10701068
&c->errbuffer : &c->outbuffer);
10711069

1072-
bufchain_prefix(buf, &data, &len);
1073-
if (len > c->remwindow)
1074-
len = c->remwindow;
1075-
if (len > c->remmaxpkt)
1076-
len = c->remmaxpkt;
1070+
ptrlen data = bufchain_prefix(buf);
1071+
if (data.len > c->remwindow)
1072+
data.len = c->remwindow;
1073+
if (data.len > c->remmaxpkt)
1074+
data.len = c->remmaxpkt;
10771075
if (buf == &c->errbuffer) {
10781076
pktout = ssh_bpp_new_pktout(
10791077
s->ppl.bpp, SSH2_MSG_CHANNEL_EXTENDED_DATA);
@@ -1083,10 +1081,10 @@ static size_t ssh2_try_send(struct ssh2_channel *c)
10831081
pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_CHANNEL_DATA);
10841082
put_uint32(pktout, c->remoteid);
10851083
}
1086-
put_string(pktout, data, len);
1084+
put_stringpl(pktout, data);
10871085
pq_push(s->ppl.out_pq, pktout);
1088-
bufchain_consume(buf, len);
1089-
c->remwindow -= len;
1086+
bufchain_consume(buf, data.len);
1087+
c->remwindow -= data.len;
10901088
}
10911089

10921090
/*
@@ -1670,11 +1668,9 @@ static void ssh2_connection_got_user_input(PacketProtocolLayer *ppl)
16701668
/*
16711669
* Add user input to the main channel's buffer.
16721670
*/
1673-
void *data;
1674-
size_t len;
1675-
bufchain_prefix(s->ppl.user_input, &data, &len);
1676-
sshfwd_write(s->mainchan_sc, data, len);
1677-
bufchain_consume(s->ppl.user_input, len);
1671+
ptrlen data = bufchain_prefix(s->ppl.user_input);
1672+
sshfwd_write(s->mainchan_sc, data.ptr, data.len);
1673+
bufchain_consume(s->ppl.user_input, data.len);
16781674
}
16791675
}
16801676

0 commit comments

Comments
 (0)