From a5df8828164272c26e88028b00e6e1ec88ee222d Mon Sep 17 00:00:00 2001 From: Gregory LEOCADIE Date: Thu, 29 Mar 2018 12:52:30 +0200 Subject: [PATCH] Fix: use off_t type for lseek function return value to avoid overflow MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Context: LTTng is configured in live mode with only one channel, getting traces for a long-running application (days of uptime) The trace file gets bigger (many GBs), so the offset (bigger than int.MaxValue). When getting a packet for such offset, the lseek returns bigger than int.MaxValue. This value is stored in a variable "ret" of type int. We have an overflow which leads to sending an error to the viewer (babeltrace), which stops. [error] get_data_packet: error. [error] get_data_packet failed [error] Unknown return code 0 Signed-off-by: Gregory LEOCADIE Signed-off-by: Jérémie Galarneau --- src/bin/lttng-relayd/live.c | 5 +++-- src/bin/lttng-relayd/main.c | 6 ++++-- src/bin/lttng-sessiond/cmd.c | 6 ++++-- src/common/utils.c | 6 ++++-- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/bin/lttng-relayd/live.c b/src/bin/lttng-relayd/live.c index 8a8550f40..a5016ac33 100644 --- a/src/bin/lttng-relayd/live.c +++ b/src/bin/lttng-relayd/live.c @@ -1483,6 +1483,7 @@ static int viewer_get_packet(struct relay_connection *conn) { int ret; + off_t lseek_ret; char *reply = NULL; struct lttng_viewer_get_packet get_packet_info; struct lttng_viewer_trace_packet reply_header; @@ -1524,9 +1525,9 @@ int viewer_get_packet(struct relay_connection *conn) } pthread_mutex_lock(&vstream->stream->lock); - ret = lseek(vstream->stream_fd->fd, be64toh(get_packet_info.offset), + lseek_ret = lseek(vstream->stream_fd->fd, be64toh(get_packet_info.offset), SEEK_SET); - if (ret < 0) { + if (lseek_ret < 0) { PERROR("lseek fd %d to offset %" PRIu64, vstream->stream_fd->fd, be64toh(get_packet_info.offset)); goto error; diff --git a/src/bin/lttng-relayd/main.c b/src/bin/lttng-relayd/main.c index e3c070f8b..a8164ff22 100644 --- a/src/bin/lttng-relayd/main.c +++ b/src/bin/lttng-relayd/main.c @@ -1588,6 +1588,7 @@ static int rotate_truncate_stream(struct relay_stream *stream) { int ret, new_fd; + off_t lseek_ret; uint64_t diff, pos = 0; char buf[FILE_COPY_BUFFER_SIZE]; @@ -1614,10 +1615,11 @@ int rotate_truncate_stream(struct relay_stream *stream) * Rewind the current tracefile to the position at which the rotation * should have occured. */ - ret = lseek(stream->stream_fd->fd, + lseek_ret = lseek(stream->stream_fd->fd, stream->pos_after_last_complete_data_index, SEEK_SET); - if (ret < 0) { + if (lseek_ret < 0) { PERROR("seek truncate stream"); + ret = -1; goto end; } diff --git a/src/bin/lttng-sessiond/cmd.c b/src/bin/lttng-sessiond/cmd.c index 534f191a8..ad635a9a9 100644 --- a/src/bin/lttng-sessiond/cmd.c +++ b/src/bin/lttng-sessiond/cmd.c @@ -3671,10 +3671,12 @@ static int clear_metadata_file(int fd) { int ret; + off_t lseek_ret; - ret = lseek(fd, 0, SEEK_SET); - if (ret < 0) { + lseek_ret = lseek(fd, 0, SEEK_SET); + if (lseek_ret < 0) { PERROR("lseek"); + ret = -1; goto end; } diff --git a/src/common/utils.c b/src/common/utils.c index 004cd8f0d..7d018b53b 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -1487,15 +1487,17 @@ LTTNG_HIDDEN int utils_truncate_stream_file(int fd, off_t length) { int ret; + off_t lseek_ret; ret = ftruncate(fd, length); if (ret < 0) { PERROR("ftruncate"); goto end; } - ret = lseek(fd, length, SEEK_SET); - if (ret < 0) { + lseek_ret = lseek(fd, length, SEEK_SET); + if (lseek_ret < 0) { PERROR("lseek"); + ret = -1; goto end; } end: -- 2.34.1