Skip to content

Commit bed0184

Browse files
duke8253shinrich
authored andcommitted
fix stringstream crash during shutdown
1 parent 2449069 commit bed0184

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

plugins/experimental/ssl_session_reuse/src/common.cc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@
3030

3131
#include "common.h"
3232

33-
const unsigned char salt[] = {115, 97, 108, 117, 0, 85, 137, 229};
33+
const unsigned char salt[] = {115, 97, 108, 117, 0, 85, 137, 229};
34+
const unsigned char hex_chars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
3435

3536
std::string
36-
hex_str(std::string str)
37+
hex_str(std::string const &str)
3738
{
38-
std::string hex_str = "";
39-
for (char &c : str) {
40-
std::stringstream stream;
41-
stream << std::hex << std::setfill('0') << std::setw(2) << (0xFF & static_cast<unsigned int>(c));
42-
hex_str += stream.str();
43-
}
44-
return hex_str;
39+
size_t len = str.size() * 2 + 1;
40+
char hex_str[len];
41+
for (unsigned long int i = 0; i < str.size(); ++i) {
42+
unsigned char c = str.at(i);
43+
hex_str[i * 2] = hex_chars[(c & 0xF0) >> 4];
44+
hex_str[i * 2 + 1] = hex_chars[(c & 0x0F)];
45+
}
46+
hex_str[len] = '\0';
47+
return std::string(hex_str, len);
4548
}
4649

4750
int

plugins/experimental/ssl_session_reuse/src/common.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#pragma once
2525

2626
#include <iostream>
27-
#include <sstream>
2827
#include <iomanip>
2928
#include <string>
3029
#include <cstring>
@@ -70,7 +69,7 @@ class PluginThreads
7069
std::mutex threads_mutex;
7170
};
7271

73-
std::string hex_str(std::string str);
72+
std::string hex_str(std::string const &str);
7473

7574
int encrypt_encode64(const unsigned char *key, int key_length, const unsigned char *in_data, int in_data_len, char *out_data,
7675
size_t out_data_size, size_t *out_data_len);

0 commit comments

Comments
 (0)