Commit 74c02f66 authored by Niels Möller's avatar Niels Möller
Browse files

* src/sftp/buffer.c (sftp_check_input): Don't modify i->left here.

(sftp_get_data): ...but do it here instead.
(sftp_free_string): Reintroduced this function.
(sftp_get_string): This function now requires the user to
deallocate the string.
(sftp_get_string_auto): Simpler function, that deallocates strings
automatically.
(sftp_read_packet): Return failure (0), rather than crashing, if
there is data leftover from the previous packet.
(sftp_put_final_length): Calculate the length correctly.
(sftp_put_reset): Fixed assertion.
(sftp_put_attrib): Likewise.

Rev: src/sftp/buffer.c:1.5
Rev: src/sftp/buffer.h:1.5
parent bf81b337
......@@ -63,20 +63,20 @@ struct sftp_output
/* Input */
static int
sftp_check_input(struct sftp_input *input, UINT32 length)
sftp_check_input(const struct sftp_input *i, UINT32 length)
{
if (input->left < length)
return 0;
input->left -= length;
return 1;
return (i->left >= length);
}
int
sftp_get_data(struct sftp_input *i, UINT32 length, UINT8 *data)
{
return sftp_check_input(i, length)
&& (fread(data, 1, length, i->f) == length);
if (sftp_check_input(i, length))
{
i->left -= length;
return (fread(data, 1, length, i->f) == length);
}
return 0;
}
#define GET_DATA(i, buf) \
......@@ -104,8 +104,6 @@ sftp_get_string(struct sftp_input *i, UINT32 *length)
{
UINT8 *data;
assert(i->used_strings < SFTP_MAX_STRINGS);
if (!(sftp_get_uint32(i, length) && sftp_check_input(i, *length)))
return NULL;
......@@ -122,6 +120,27 @@ sftp_get_string(struct sftp_input *i, UINT32 *length)
/* NUL-terminate, for convenience */
data[*length] = '\0';
return data;
}
void
sftp_free_string(UINT8 *s)
{
free(s);
}
UINT8 *
sftp_get_string_auto(struct sftp_input *i, UINT32 *length)
{
UINT8 *data;
assert(i->used_strings < SFTP_MAX_STRINGS);
data = sftp_get_string(i, length);
if (!data)
return NULL;
/* Remember the string. */
i->strings[i->used_strings++] = data;
return data;
......@@ -147,13 +166,13 @@ sftp_make_input(FILE *f)
return i;
}
static void
void
sftp_input_clear_strings(struct sftp_input *i)
{
unsigned k;
for (k = 0; k < i->used_strings; k++)
free(i->strings[k]);
sftp_free_string(i->strings[k]);
i->used_strings = 0;
}
......@@ -165,7 +184,8 @@ sftp_read_packet(struct sftp_input *i)
UINT8 buf[4];
int done;
assert(i->left == 0);
if (i->left)
return 0;
/* First, deallocate the strings. */
sftp_input_clear_strings(i);
......@@ -264,15 +284,14 @@ void
sftp_put_final_length(struct sftp_output *o,
UINT32 index)
{
sftp_put_length(o, index, o->i - index);
sftp_put_length(o, index, o->i - index - 4);
}
void
sftp_put_reset(struct sftp_output *o,
UINT32 index)
{
assert(index >= o->i);
assert(index <= o->size);
assert(index < o->i);
o->i = index;
}
......@@ -550,7 +569,7 @@ sftp_get_attrib(struct sftp_input *i, struct sftp_attrib *a)
void
sftp_put_attrib(struct sftp_output *o, const struct sftp_attrib *a)
{
assert(!a->flags & SSH_FILEXFER_ATTR_EXTENDED);
assert(!(a->flags & SSH_FILEXFER_ATTR_EXTENDED));
sftp_put_uint32(o, a->flags);
......
......@@ -75,6 +75,17 @@ sftp_get_uint64(struct sftp_input *i, off_t *value);
UINT8 *
sftp_get_string(struct sftp_input *i, UINT32 *length);
void
sftp_free_string(UINT8 *s);
/* Like sftp_get_string, but the data is deallocated automatically by
* sftp_read_packet and sftp_input_clear_strings. */
UINT8 *
sftp_get_string_auto(struct sftp_input *i, UINT32 *length);
void
sftp_input_clear_strings(struct sftp_input *i);
int
sftp_get_eod(struct sftp_input *i);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment