summaryrefslogtreecommitdiff
path: root/src/or/channel.c
diff options
context:
space:
mode:
authorDavid Goulet <dgoulet@torproject.org>2017-12-07 15:41:09 -0500
committerNick Mathewson <nickm@torproject.org>2017-12-08 14:43:27 -0500
commit6120efd771928fc958b552b9f5c3e09d949e92fe (patch)
tree1ce64d9813387af6384f35d71224baf3a32252c0 /src/or/channel.c
parent428ee55e5187a57b8bbc171c8b62da08209a7954 (diff)
downloadtor-6120efd771928fc958b552b9f5c3e09d949e92fe.tar.gz
tor-6120efd771928fc958b552b9f5c3e09d949e92fe.zip
chan: Do not re-queue after a fail cell write
Couple things happen in this commit. First, we do not re-queue a cell back in the circuit queue if the write packed cell failed. Currently, it is close to impossible to have it failed but just in case, the channel is mark as closed and we move on. The second thing is that the channel_write_packed_cell() always took ownership of the cell whatever the outcome. This means, on success or failure, it needs to free it. It turns out that that we were using the wrong free function in one case and not freeing it in an other possible code path. So, this commit makes sure we only free it in one place that is at the very end of channel_write_packed_cell() which is the top layer of the channel abstraction. This makes also channel_tls_write_packed_cell_method() return a negative value on error. Two unit tests had to be fixed (quite trivial) due to a double free of the packed cell in the test since now we do free it in all cases correctly. Part of #23709 Signed-off-by: David Goulet <dgoulet@torproject.org>
Diffstat (limited to 'src/or/channel.c')
-rw-r--r--src/or/channel.c24
1 files changed, 19 insertions, 5 deletions
diff --git a/src/or/channel.c b/src/or/channel.c
index 94d7af47b5..9e79fc1a3f 100644
--- a/src/or/channel.c
+++ b/src/or/channel.c
@@ -1431,8 +1431,11 @@ channel_clear_remote_end(channel_t *chan)
/**
* Write to a channel the given packed cell.
*
- * Return 0 on success and the cell is freed so the caller MUST discard any
- * reference to it. On error, -1 is returned and the cell is untouched.
+ * Return 0 on success or -1 on error.
+ *
+ * Two possible errors can happen. Either the channel is not opened or the
+ * lower layer (specialized channel) failed to write it. In both cases, it is
+ * the caller responsability to free the cell.
*/
static int
write_packed_cell(channel_t *chan, packed_cell_t *cell)
@@ -1483,10 +1486,15 @@ write_packed_cell(channel_t *chan, packed_cell_t *cell)
* Write a packed cell to a channel using the write_cell() method. This is
* called by the transport-independent code to deliver a packed cell to a
* channel for transmission.
+ *
+ * Return 0 on success else a negative value. In both cases, the caller should
+ * not access the cell anymore, it is freed both on success and error.
*/
int
channel_write_packed_cell(channel_t *chan, packed_cell_t *cell)
{
+ int ret = -1;
+
tor_assert(chan);
tor_assert(cell);
@@ -1494,14 +1502,20 @@ channel_write_packed_cell(channel_t *chan, packed_cell_t *cell)
log_debug(LD_CHANNEL, "Discarding %p on closing channel %p with "
"global ID "U64_FORMAT, cell, chan,
U64_PRINTF_ARG(chan->global_identifier));
- tor_free(cell);
- return -1;
+ goto end;
}
log_debug(LD_CHANNEL,
"Writing %p to channel %p with global ID "
U64_FORMAT, cell, chan, U64_PRINTF_ARG(chan->global_identifier));
- return write_packed_cell(chan, cell);
+ ret = write_packed_cell(chan, cell);
+
+ end:
+ /* Whatever happens, we free the cell. Either an error occured or the cell
+ * was put on the connection outbuf, both cases we have ownership of the
+ * cell and we free it. */
+ packed_cell_free(cell);
+ return ret;
}
/**