Try to fix some overflow issues on 32-bit platforms, where size_t is 32-bit. Untested.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 7 Apr 2018 10:04:38 +0000 (12:04 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 7 Apr 2018 10:04:38 +0000 (12:04 +0200)
client.h
server.cpp
stream.cpp
stream.h

index f2bd7fb..c18df4a 100644 (file)
--- a/client.h
+++ b/client.h
@@ -104,25 +104,25 @@ struct Client {
        // although only at a keyframe.
        // -2 means we want to send from the _beginning_ of the backlog.
        // -3 means we sent the header only.
-       static const size_t STREAM_POS_AT_END = -1;
-       static const size_t STREAM_POS_AT_START = -2;
-       static const size_t STREAM_POS_HEADER_ONLY = -3;
+       static const uint64_t STREAM_POS_AT_END = -1;
+       static const uint64_t STREAM_POS_AT_START = -2;
+       static const uint64_t STREAM_POS_HEADER_ONLY = -3;
 
        // Once we go into WAITING_FOR_KEYFRAME, PREBUFFERING or SENDING_DATA,
        // these negative values will be translated to real numbers.
-       size_t stream_pos = 0;
+       uint64_t stream_pos = 0;
 
        // Position at which to end the stream (one-past-the-end, used for fragments).
        // -1 means never to end; this is the common case.
-       static const size_t STREAM_POS_NO_END = -1;
-       size_t stream_pos_end = 0;
+       static const uint64_t STREAM_POS_NO_END = -1;
+       uint64_t stream_pos_end = 0;
 
        // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
-       size_t bytes_sent = 0;
+       uint64_t bytes_sent = 0;
 
        // Number of times we've skipped forward due to the backlog being too big,
        // and how many bytes we've skipped over in all. Only relevant for SENDING_DATA.
-       size_t bytes_lost = 0, num_loss_events = 0;
+       uint64_t bytes_lost = 0, num_loss_events = 0;
 
        TLSContext *tls_context = nullptr;
        const unsigned char *tls_data_to_send = nullptr;
index be583e4..ceb7f3d 100644 (file)
@@ -1,5 +1,6 @@
 #include <assert.h>
 #include <errno.h>
+#include <inttypes.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
 #include <pthread.h>
@@ -1069,7 +1070,7 @@ void Server::construct_stream_header(Client *client)
                response.append(buf);
        } else if (client->stream_pos_end != Client::STREAM_POS_NO_END) {
                char buf[64];
-               snprintf(buf, sizeof(buf), "Content-length: %zu\r\n", client->stream_pos_end - client->stream_pos);
+               snprintf(buf, sizeof(buf), "Content-length: %" PRIu64 "\r\n", client->stream_pos_end - client->stream_pos);
                response.append(buf);
        }
        if (client->http_11) {
index 62c7507..07d6ae9 100644 (file)
@@ -1,5 +1,6 @@
 #include <assert.h>
 #include <errno.h>
+#include <inttypes.h>
 #include <limits.h>
 #include <math.h>
 #include <netinet/in.h>
@@ -22,7 +23,7 @@ using namespace std;
 
 Stream::Stream(const string &url,
                size_t backlog_size,
-               size_t prebuffering_bytes,
+               uint64_t prebuffering_bytes,
                Encoding encoding,
                Encoding src_encoding,
                unsigned hls_frag_duration,
@@ -421,8 +422,8 @@ shared_ptr<const string> Stream::generate_hls_playlist(bool http_11, bool close_
                "#EXTM3U\r\n"
                "#EXT-X-VERSION:7\r\n"
                "#EXT-X-TARGETDURATION:%u\r\n"
-               "#EXT-X-MEDIA-SEQUENCE:%zu\r\n"
-               "#EXT-X-DISCONTINUITY-SEQUENCE:%zu\r\n",
+               "#EXT-X-MEDIA-SEQUENCE:%" PRIu64 "\r\n"
+               "#EXT-X-DISCONTINUITY-SEQUENCE:%" PRIu64 "\r\n",
                hls_frag_duration,
                first_fragment_index,
                discontinuity_counter);
@@ -438,7 +439,7 @@ shared_ptr<const string> Stream::generate_hls_playlist(bool http_11, bool close_
        if (fragments.size() >= 3) {
                for (size_t i = 0; i < fragments.size() - 2; ++i) {
                        char buf[256];
-                       snprintf(buf, sizeof(buf), "#EXTINF:%f,\r\n%s?frag=%zu-%zu\r\n",
+                       snprintf(buf, sizeof(buf), "#EXTINF:%f,\r\n%s?frag=%" PRIu64 "-%" PRIu64 "\r\n",
                                fragments[i + 1].pts - fragments[i].pts,
                                url.c_str(),
                                fragments[i].byte_position,
index 77e9d8a..8d249c3 100644 (file)
--- a/stream.h
+++ b/stream.h
@@ -31,7 +31,7 @@ struct Stream {
 
        Stream(const std::string &url,
               size_t backlog_size,
-              size_t prebuffering_bytes,
+              uint64_t prebuffering_bytes,
               Encoding encoding,
               Encoding src_encoding,
               unsigned hls_frag_duration,
@@ -88,15 +88,15 @@ struct Stream {
        // This is basically to force a buffer on the client, which can help
        // if the client expects us to be able to fill up the buffer much
        // faster than realtime (ie., it expects a static file).
-       size_t prebuffering_bytes;
+       uint64_t prebuffering_bytes;
 
        // How many bytes this stream have received. Can very well be larger
        // than <backlog_size>, since the buffer wraps.
-       size_t bytes_received = 0;
+       uint64_t bytes_received = 0;
 
        // A list of points in the stream that is suitable to start new clients at
        // (after having sent the header). Empty if no such point exists yet.
-       std::deque<size_t> suitable_starting_points;
+       std::deque<uint64_t> suitable_starting_points;
 
        // A list of HLS fragment boundaries currently in the backlog; the first fragment
        // is between point 0 and 1, the second is between 1 and 2, and so on.
@@ -111,7 +111,7 @@ struct Stream {
        // extended and thus should not be output. So the last fragment output is
        // from points N-3..N-2.
        struct FragmentStart {
-               size_t byte_position;
+               uint64_t byte_position;
                double pts;
        };
        std::deque<FragmentStart> fragments;