Make sure relevant std::stringstreams are set to binary

This commit is contained in:
sfan5 2021-09-11 21:06:57 +02:00 committed by GitHub
parent 766e885a1b
commit 75bf9b75ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 54 additions and 66 deletions

@ -1693,13 +1693,13 @@ float Client::mediaReceiveProgress()
return 1.0; // downloader only exists when not yet done return 1.0; // downloader only exists when not yet done
} }
typedef struct TextureUpdateArgs { struct TextureUpdateArgs {
gui::IGUIEnvironment *guienv; gui::IGUIEnvironment *guienv;
u64 last_time_ms; u64 last_time_ms;
u16 last_percent; u16 last_percent;
const wchar_t* text_base; const wchar_t* text_base;
ITextureSource *tsrc; ITextureSource *tsrc;
} TextureUpdateArgs; };
void Client::showUpdateProgressTexture(void *args, u32 progress, u32 max_progress) void Client::showUpdateProgressTexture(void *args, u32 progress, u32 max_progress)
{ {
@ -1718,8 +1718,8 @@ void Client::showUpdateProgressTexture(void *args, u32 progress, u32 max_progres
if (do_draw) { if (do_draw) {
targs->last_time_ms = time_ms; targs->last_time_ms = time_ms;
std::basic_stringstream<wchar_t> strm; std::wostringstream strm;
strm << targs->text_base << " " << targs->last_percent << "%..."; strm << targs->text_base << L" " << targs->last_percent << L"%...";
m_rendering_engine->draw_load_screen(strm.str(), targs->guienv, targs->tsrc, 0, m_rendering_engine->draw_load_screen(strm.str(), targs->guienv, targs->tsrc, 0,
72 + (u16) ((18. / 100.) * (double) targs->last_percent), true); 72 + (u16) ((18. / 100.) * (double) targs->last_percent), true);
} }

@ -1618,7 +1618,7 @@ bool Game::getServerContent(bool *aborted)
dtime, progress); dtime, progress);
delete[] text; delete[] text;
} else { } else {
std::stringstream message; std::ostringstream message;
std::fixed(message); std::fixed(message);
message.precision(0); message.precision(0);
float receive = client->mediaReceiveProgress() * 100; float receive = client->mediaReceiveProgress() * 100;

@ -70,11 +70,11 @@ bool Database_LevelDB::saveBlock(const v3s16 &pos, const std::string &data)
void Database_LevelDB::loadBlock(const v3s16 &pos, std::string *block) void Database_LevelDB::loadBlock(const v3s16 &pos, std::string *block)
{ {
std::string datastr;
leveldb::Status status = m_database->Get(leveldb::ReadOptions(), leveldb::Status status = m_database->Get(leveldb::ReadOptions(),
i64tos(getBlockAsInteger(pos)), &datastr); i64tos(getBlockAsInteger(pos)), block);
*block = (status.ok()) ? datastr : ""; if (!status.ok())
block->clear();
} }
bool Database_LevelDB::deleteBlock(const v3s16 &pos) bool Database_LevelDB::deleteBlock(const v3s16 &pos)
@ -131,7 +131,7 @@ void PlayerDatabaseLevelDB::savePlayer(RemotePlayer *player)
std::string (long) serialized_inventory std::string (long) serialized_inventory
*/ */
std::ostringstream os; std::ostringstream os(std::ios_base::binary);
writeU8(os, 1); writeU8(os, 1);
PlayerSAO *sao = player->getPlayerSAO(); PlayerSAO *sao = player->getPlayerSAO();
@ -142,7 +142,7 @@ void PlayerDatabaseLevelDB::savePlayer(RemotePlayer *player)
writeF32(os, sao->getRotation().Y); writeF32(os, sao->getRotation().Y);
writeU16(os, sao->getBreath()); writeU16(os, sao->getBreath());
StringMap stringvars = sao->getMeta().getStrings(); const auto &stringvars = sao->getMeta().getStrings();
writeU32(os, stringvars.size()); writeU32(os, stringvars.size());
for (const auto &it : stringvars) { for (const auto &it : stringvars) {
os << serializeString16(it.first); os << serializeString16(it.first);
@ -170,7 +170,7 @@ bool PlayerDatabaseLevelDB::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
player->getName(), &raw); player->getName(), &raw);
if (!s.ok()) if (!s.ok())
return false; return false;
std::istringstream is(raw); std::istringstream is(raw, std::ios_base::binary);
if (readU8(is) > 1) if (readU8(is) > 1)
return false; return false;
@ -230,7 +230,7 @@ bool AuthDatabaseLevelDB::getAuth(const std::string &name, AuthEntry &res)
leveldb::Status s = m_database->Get(leveldb::ReadOptions(), name, &raw); leveldb::Status s = m_database->Get(leveldb::ReadOptions(), name, &raw);
if (!s.ok()) if (!s.ok())
return false; return false;
std::istringstream is(raw); std::istringstream is(raw, std::ios_base::binary);
/* /*
u8 version = 1 u8 version = 1
@ -262,7 +262,7 @@ bool AuthDatabaseLevelDB::getAuth(const std::string &name, AuthEntry &res)
bool AuthDatabaseLevelDB::saveAuth(const AuthEntry &authEntry) bool AuthDatabaseLevelDB::saveAuth(const AuthEntry &authEntry)
{ {
std::ostringstream os; std::ostringstream os(std::ios_base::binary);
writeU8(os, 1); writeU8(os, 1);
os << serializeString16(authEntry.password); os << serializeString16(authEntry.password);

@ -274,10 +274,10 @@ void MapDatabasePostgreSQL::loadBlock(const v3s16 &pos, std::string *block)
PGresult *results = execPrepared("read_block", ARRLEN(args), args, PGresult *results = execPrepared("read_block", ARRLEN(args), args,
argLen, argFmt, false); argLen, argFmt, false);
*block = "";
if (PQntuples(results)) if (PQntuples(results))
*block = std::string(PQgetvalue(results, 0, 0), PQgetlength(results, 0, 0)); block->assign(PQgetvalue(results, 0, 0), PQgetlength(results, 0, 0));
else
block->clear();
PQclear(results); PQclear(results);
} }
@ -496,6 +496,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
execPrepared("remove_player_inventory_items", 1, rmvalues); execPrepared("remove_player_inventory_items", 1, rmvalues);
std::vector<const InventoryList*> inventory_lists = sao->getInventory()->getLists(); std::vector<const InventoryList*> inventory_lists = sao->getInventory()->getLists();
std::ostringstream oss;
for (u16 i = 0; i < inventory_lists.size(); i++) { for (u16 i = 0; i < inventory_lists.size(); i++) {
const InventoryList* list = inventory_lists[i]; const InventoryList* list = inventory_lists[i];
const std::string &name = list->getName(); const std::string &name = list->getName();
@ -512,9 +513,10 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
execPrepared("add_player_inventory", 5, inv_values); execPrepared("add_player_inventory", 5, inv_values);
for (u32 j = 0; j < list->getSize(); j++) { for (u32 j = 0; j < list->getSize(); j++) {
std::ostringstream os; oss.str("");
list->getItem(j).serialize(os); oss.clear();
std::string itemStr = os.str(), slotId = itos(j); list->getItem(j).serialize(oss);
std::string itemStr = oss.str(), slotId = itos(j);
const char* invitem_values[] = { const char* invitem_values[] = {
player->getName(), player->getName(),

@ -127,8 +127,7 @@ void Database_Redis::loadBlock(const v3s16 &pos, std::string *block)
switch (reply->type) { switch (reply->type) {
case REDIS_REPLY_STRING: { case REDIS_REPLY_STRING: {
*block = std::string(reply->str, reply->len); block->assign(reply->str, reply->len);
// std::string copies the memory so this won't cause any problems
freeReplyObject(reply); freeReplyObject(reply);
return; return;
} }
@ -141,8 +140,7 @@ void Database_Redis::loadBlock(const v3s16 &pos, std::string *block)
"Redis command 'HGET %s %s' errored: ") + errstr); "Redis command 'HGET %s %s' errored: ") + errstr);
} }
case REDIS_REPLY_NIL: { case REDIS_REPLY_NIL: {
*block = ""; block->clear();
// block not found in database
freeReplyObject(reply); freeReplyObject(reply);
return; return;
} }

@ -302,7 +302,10 @@ void MapDatabaseSQLite3::loadBlock(const v3s16 &pos, std::string *block)
const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0); const char *data = (const char *) sqlite3_column_blob(m_stmt_read, 0);
size_t len = sqlite3_column_bytes(m_stmt_read, 0); size_t len = sqlite3_column_bytes(m_stmt_read, 0);
*block = (data) ? std::string(data, len) : ""; if (data)
block->assign(data, len);
else
block->clear();
sqlite3_step(m_stmt_read); sqlite3_step(m_stmt_read);
// We should never get more than 1 row, so ok to reset // We should never get more than 1 row, so ok to reset
@ -491,6 +494,7 @@ void PlayerDatabaseSQLite3::savePlayer(RemotePlayer *player)
sqlite3_reset(m_stmt_player_remove_inventory_items); sqlite3_reset(m_stmt_player_remove_inventory_items);
std::vector<const InventoryList*> inventory_lists = sao->getInventory()->getLists(); std::vector<const InventoryList*> inventory_lists = sao->getInventory()->getLists();
std::ostringstream oss;
for (u16 i = 0; i < inventory_lists.size(); i++) { for (u16 i = 0; i < inventory_lists.size(); i++) {
const InventoryList* list = inventory_lists[i]; const InventoryList* list = inventory_lists[i];
@ -503,9 +507,10 @@ void PlayerDatabaseSQLite3::savePlayer(RemotePlayer *player)
sqlite3_reset(m_stmt_player_add_inventory); sqlite3_reset(m_stmt_player_add_inventory);
for (u32 j = 0; j < list->getSize(); j++) { for (u32 j = 0; j < list->getSize(); j++) {
std::ostringstream os; oss.str("");
list->getItem(j).serialize(os); oss.clear();
std::string itemStr = os.str(); list->getItem(j).serialize(oss);
std::string itemStr = oss.str();
str_to_sqlite(m_stmt_player_add_inventory_items, 1, player->getName()); str_to_sqlite(m_stmt_player_add_inventory_items, 1, player->getName());
int_to_sqlite(m_stmt_player_add_inventory_items, 2, i); int_to_sqlite(m_stmt_player_add_inventory_items, 2, i);

@ -729,7 +729,6 @@ void GUIChatConsole::middleClick(s32 col, s32 row)
msg << gettext("Failed to open webpage"); msg << gettext("Failed to open webpage");
} }
msg << " '" << weblink << "'"; msg << " '" << weblink << "'";
msg.flush();
m_chat_backend->addUnparsedMessage(utf8_to_wide(msg.str())); m_chat_backend->addUnparsedMessage(utf8_to_wide(msg.str()));
} }
} }

@ -3933,9 +3933,7 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode)
} }
if (e != 0) { if (e != 0) {
std::stringstream ss; fields[name] = itos(e->getActiveTab() + 1);
ss << (e->getActiveTab() +1);
fields[name] = ss.str();
} }
} else if (s.ftype == f_CheckBox) { } else if (s.ftype == f_CheckBox) {
// No dynamic cast possible due to some distributions shipped // No dynamic cast possible due to some distributions shipped
@ -3961,12 +3959,10 @@ void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode)
e = static_cast<GUIScrollBar *>(element); e = static_cast<GUIScrollBar *>(element);
if (e) { if (e) {
std::stringstream os;
os << e->getPos();
if (s.fdefault == L"Changed") if (s.fdefault == L"Changed")
fields[name] = "CHG:" + os.str(); fields[name] = "CHG:" + itos(e->getPos());
else else
fields[name] = "VAL:" + os.str(); fields[name] = "VAL:" + itos(e->getPos());
} }
} else if (s.ftype == f_AnimatedImage) { } else if (s.ftype == f_AnimatedImage) {
// No dynamic cast possible due to some distributions shipped // No dynamic cast possible due to some distributions shipped

@ -460,7 +460,6 @@ void InventoryList::deSerialize(std::istream &is)
std::getline(is, line, '\n'); std::getline(is, line, '\n');
std::istringstream iss(line); std::istringstream iss(line);
//iss.imbue(std::locale("C"));
std::string name; std::string name;
std::getline(iss, name, ' '); std::getline(iss, name, ' ');

@ -60,7 +60,7 @@ bool ItemStackMetadata::setString(const std::string &name, const std::string &va
void ItemStackMetadata::serialize(std::ostream &os) const void ItemStackMetadata::serialize(std::ostream &os) const
{ {
std::ostringstream os2; std::ostringstream os2(std::ios_base::binary);
os2 << DESERIALIZE_START; os2 << DESERIALIZE_START;
for (const auto &stringvar : m_stringvars) { for (const auto &stringvar : m_stringvars) {
if (!stringvar.first.empty() || !stringvar.second.empty()) if (!stringvar.first.empty() || !stringvar.second.empty())

@ -261,7 +261,7 @@ void Client::handleCommand_NodemetaChanged(NetworkPacket *pkt)
return; return;
std::istringstream is(pkt->readLongString(), std::ios::binary); std::istringstream is(pkt->readLongString(), std::ios::binary);
std::stringstream sstr; std::stringstream sstr(std::ios::binary);
decompressZlib(is, sstr); decompressZlib(is, sstr);
NodeMetadataList meta_updates_list(false); NodeMetadataList meta_updates_list(false);
@ -760,12 +760,11 @@ void Client::handleCommand_NodeDef(NetworkPacket* pkt)
// Decompress node definitions // Decompress node definitions
std::istringstream tmp_is(pkt->readLongString(), std::ios::binary); std::istringstream tmp_is(pkt->readLongString(), std::ios::binary);
std::ostringstream tmp_os; std::stringstream tmp_os(std::ios::binary | std::ios::in | std::ios::out);
decompressZlib(tmp_is, tmp_os); decompressZlib(tmp_is, tmp_os);
// Deserialize node definitions // Deserialize node definitions
std::istringstream tmp_is2(tmp_os.str()); m_nodedef->deSerialize(tmp_os);
m_nodedef->deSerialize(tmp_is2);
m_nodedef_received = true; m_nodedef_received = true;
} }
@ -780,12 +779,11 @@ void Client::handleCommand_ItemDef(NetworkPacket* pkt)
// Decompress item definitions // Decompress item definitions
std::istringstream tmp_is(pkt->readLongString(), std::ios::binary); std::istringstream tmp_is(pkt->readLongString(), std::ios::binary);
std::ostringstream tmp_os; std::stringstream tmp_os(std::ios::binary | std::ios::in | std::ios::out);
decompressZlib(tmp_is, tmp_os); decompressZlib(tmp_is, tmp_os);
// Deserialize node definitions // Deserialize node definitions
std::istringstream tmp_is2(tmp_os.str()); m_itemdef->deSerialize(tmp_os);
m_itemdef->deSerialize(tmp_is2);
m_itemdef_received = true; m_itemdef_received = true;
} }

@ -76,10 +76,7 @@ static void set_vector_metatable(lua_State *L)
void push_float_string(lua_State *L, float value) void push_float_string(lua_State *L, float value)
{ {
std::stringstream ss; auto str = ftos(value);
std::string str;
ss << value;
str = ss.str();
lua_pushstring(L, str.c_str()); lua_pushstring(L, str.c_str());
} }

@ -752,9 +752,7 @@ int ModApiMapgen::l_get_mapgen_params(lua_State *L)
lua_setfield(L, -2, "mgname"); lua_setfield(L, -2, "mgname");
settingsmgr->getMapSetting("seed", &value); settingsmgr->getMapSetting("seed", &value);
std::istringstream ss(value); u64 seed = from_string<u64>(value);
u64 seed;
ss >> seed;
lua_pushinteger(L, seed); lua_pushinteger(L, seed);
lua_setfield(L, -2, "seed"); lua_setfield(L, -2, "seed");

@ -272,11 +272,11 @@ int ModApiUtil::l_compress(lua_State *L)
const char *data = luaL_checklstring(L, 1, &size); const char *data = luaL_checklstring(L, 1, &size);
int level = -1; int level = -1;
if (!lua_isnone(L, 3) && !lua_isnil(L, 3)) if (!lua_isnoneornil(L, 3))
level = readParam<float>(L, 3); level = readParam<int>(L, 3);
std::ostringstream os; std::ostringstream os(std::ios_base::binary);
compressZlib(std::string(data, size), os, level); compressZlib(reinterpret_cast<const u8 *>(data), size, os, level);
std::string out = os.str(); std::string out = os.str();
@ -292,8 +292,8 @@ int ModApiUtil::l_decompress(lua_State *L)
size_t size; size_t size;
const char *data = luaL_checklstring(L, 1, &size); const char *data = luaL_checklstring(L, 1, &size);
std::istringstream is(std::string(data, size)); std::istringstream is(std::string(data, size), std::ios_base::binary);
std::ostringstream os; std::ostringstream os(std::ios_base::binary);
decompressZlib(is, os); decompressZlib(is, os);
std::string out = os.str(); std::string out = os.str();

@ -537,11 +537,8 @@ float Settings::getFloat(const std::string &name) const
u64 Settings::getU64(const std::string &name) const u64 Settings::getU64(const std::string &name) const
{ {
u64 value = 0;
std::string s = get(name); std::string s = get(name);
std::istringstream ss(s); return from_string<u64>(s);
ss >> value;
return value;
} }

@ -135,7 +135,7 @@ void TestAreaStore::testSerialization()
b.data = "Area BB"; b.data = "Area BB";
store.insertArea(&b); store.insertArea(&b);
std::ostringstream os; std::ostringstream os(std::ios_base::binary);
store.serialize(os); store.serialize(os);
std::string str = os.str(); std::string str = os.str();
@ -157,7 +157,7 @@ void TestAreaStore::testSerialization()
UASSERTEQ(const std::string &, str, str_wanted); UASSERTEQ(const std::string &, str, str_wanted);
std::istringstream is(str); std::istringstream is(str, std::ios_base::binary);
store.deserialize(is); store.deserialize(is);
// deserialize() doesn't clear the store // deserialize() doesn't clear the store

@ -248,7 +248,7 @@ std::string serializeJsonStringIfNeeded(const std::string &s)
std::string deSerializeJsonStringIfNeeded(std::istream &is) std::string deSerializeJsonStringIfNeeded(std::istream &is)
{ {
std::ostringstream tmp_os; std::stringstream tmp_os(std::ios_base::binary | std::ios_base::in | std::ios_base::out);
bool expect_initial_quote = true; bool expect_initial_quote = true;
bool is_json = false; bool is_json = false;
bool was_backslash = false; bool was_backslash = false;
@ -280,8 +280,7 @@ std::string deSerializeJsonStringIfNeeded(std::istream &is)
expect_initial_quote = false; expect_initial_quote = false;
} }
if (is_json) { if (is_json) {
std::istringstream tmp_is(tmp_os.str(), std::ios::binary); return deSerializeJsonString(tmp_os);
return deSerializeJsonString(tmp_is);
} }
return tmp_os.str(); return tmp_os.str();