1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-22 10:19:57 +01:00

Use iobuf buffer size for temporary buffer size

* common/iobuf.c (iobuf_copy): Use iobuf buffer size for temporary
buffers.
* g10/plaintext.c (handle_plaintext, do_hash): Likewise.
* g10/sign.c (sign_file): Likewise.
--

As iobuf will have zerocopy operation for read/write, it is better to
use same size buffers as iobuf for temporary copy buffers.

GnuPG-bug-id: T5828
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
This commit is contained in:
Jussi Kivilinna 2022-02-12 20:37:48 +02:00
parent 70b738f93f
commit f2322ff942
3 changed files with 19 additions and 13 deletions

View File

@ -2298,9 +2298,7 @@ size_t
iobuf_copy (iobuf_t dest, iobuf_t source)
{
char *temp;
/* Use a 32 KB buffer. */
const size_t temp_size = 32 * 1024;
size_t temp_size;
size_t nread;
size_t nwrote = 0;
size_t max_read = 0;
@ -2312,6 +2310,9 @@ iobuf_copy (iobuf_t dest, iobuf_t source)
if (iobuf_error (dest))
return -1;
/* Use iobuf buffer size for temporary buffer. */
temp_size = iobuf_set_buffer_size(0) * 1024;
temp = xmalloc (temp_size);
while (1)
{

View File

@ -291,10 +291,11 @@ handle_plaintext (PKT_plaintext * pt, md_filter_context_t * mfx,
}
else /* Binary mode. */
{
byte *buffer = xmalloc (32768);
size_t temp_size = iobuf_set_buffer_size(0) * 1024;
byte *buffer = xmalloc (temp_size);
while (pt->len)
{
int len = pt->len > 32768 ? 32768 : pt->len;
int len = pt->len > temp_size ? temp_size : pt->len;
len = iobuf_read (pt->buf, buffer, len);
if (len == -1)
{
@ -366,10 +367,11 @@ handle_plaintext (PKT_plaintext * pt, md_filter_context_t * mfx,
}
else
{ /* binary mode */
size_t temp_size = iobuf_set_buffer_size(0) * 1024;
byte *buffer;
int eof_seen = 0;
buffer = xtrymalloc (32768);
buffer = xtrymalloc (temp_size);
if (!buffer)
{
err = gpg_error_from_syserror ();
@ -378,16 +380,16 @@ handle_plaintext (PKT_plaintext * pt, md_filter_context_t * mfx,
while (!eof_seen)
{
/* Why do we check for len < 32768:
/* Why do we check for len < temp_size:
* If we won't, we would practically read 2 EOFs but
* the first one has already popped the block_filter
* off and therefore we don't catch the boundary.
* So, always assume EOF if iobuf_read returns less bytes
* then requested */
int len = iobuf_read (pt->buf, buffer, 32768);
int len = iobuf_read (pt->buf, buffer, temp_size);
if (len == -1)
break;
if (len < 32768)
if (len < temp_size)
eof_seen = 1;
if (mfx->md)
gcry_md_write (mfx->md, buffer, len);
@ -545,10 +547,11 @@ do_hash (gcry_md_hd_t md, gcry_md_hd_t md2, IOBUF fp, int textmode)
}
else
{
byte *buffer = xmalloc (32768);
size_t temp_size = iobuf_set_buffer_size(0) * 1024;
byte *buffer = xmalloc (temp_size);
int ret;
while ((ret = iobuf_read (fp, buffer, 32768)) != -1)
while ((ret = iobuf_read (fp, buffer, temp_size)) != -1)
{
if (md)
gcry_md_write (md, buffer, ret);

View File

@ -1294,6 +1294,8 @@ sign_file (ctrl_t ctrl, strlist_t filenames, int detached, strlist_t locusr,
/* Setup the inner packet. */
if (detached)
{
size_t iobuf_size = iobuf_set_buffer_size(0) * 1024;
if (multifile)
{
strlist_t sl;
@ -1328,7 +1330,7 @@ sign_file (ctrl_t ctrl, strlist_t filenames, int detached, strlist_t locusr,
iobuf_push_filter (inp, text_filter, &tfx);
}
iobuf_push_filter (inp, md_filter, &mfx);
while (iobuf_read (inp, NULL, 1<<30) != -1)
while (iobuf_read (inp, NULL, iobuf_size) != -1)
;
iobuf_close (inp);
inp = NULL;
@ -1339,7 +1341,7 @@ sign_file (ctrl_t ctrl, strlist_t filenames, int detached, strlist_t locusr,
else
{
/* Read, so that the filter can calculate the digest. */
while (iobuf_read (inp, NULL, 1<<30) != -1)
while (iobuf_read (inp, NULL, iobuf_size) != -1)
;
}
}