1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Fix the snprintf size issue.

This commit is contained in:
winlin 2022-08-10 08:32:02 +08:00
parent 1ab584b2ae
commit a71eddd56a
10 changed files with 46 additions and 17 deletions

View file

@ -178,11 +178,15 @@ srs_error_t SrsLatestVersion::start()
uuid_t uuid;
uuid_generate_time(uuid);
char buf[32];
// Must reserve last 1 byte for the trailing '\0', because we expect the size of uuid string is 32 bytes.
char buf[32 + 1];
srs_assert(16 == sizeof(uuid_t));
for (int i = 0; i < 16; i++) {
snprintf(buf + i * 2, sizeof(buf), "%02x", uuid[i]);
int r0 = snprintf(buf + i * 2, sizeof(buf) - i * 2, "%02x", uuid[i]);
srs_assert(r0 > 0 && r0 < sizeof(buf) - i * 2);
}
server_id_ = string(buf, sizeof(buf));
server_id_ = buf;
}
return trd_->start();

View file

@ -433,9 +433,12 @@ std::string SrsUdpMuxSocket::peer_id()
peer_port = atoi(port_string);
}
// Build the peer id.
static char id_buf[128];
// Build the peer id, reserve 1 byte for the trailing '\0'.
static char id_buf[128 + 1];
int len = snprintf(id_buf, sizeof(id_buf), "%s:%d", peer_ip.c_str(), peer_port);
if (len <= 0 || len >= sizeof(id_buf)) {
return "";
}
peer_id_ = string(id_buf, len);
// Update the stat.

View file

@ -1386,7 +1386,7 @@ string srs_string_dumps_hex(const char* str, int length, int limit, char seperat
int len = 0;
for (int i = 0; i < length && i < limit && len < LIMIT; ++i) {
int nb = snprintf(buf + len, LIMIT - len, "%02x", (uint8_t)str[i]);
if (nb < 0 || nb >= LIMIT - len) {
if (nb <= 0 || nb >= LIMIT - len) {
break;
}
len += nb;