0

[sql] Use base::Contains() instead of std::find() in //sql

Use base::Contains() instead of std::find() where appropriate in
//sql.

Bug: 561800
Change-Id: I8ce90854aeab6d12fc5c97e5d36691b625f2c2f1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4755164
Commit-Queue: Evan Stade <estade@chromium.org>
Reviewed-by: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1180671}
This commit is contained in:
Ho Cheung
2023-08-08 03:41:10 +00:00
committed by Chromium LUCI CQ
parent 2e8970d5c2
commit 4752435bd0
4 changed files with 26 additions and 22 deletions

@ -14,6 +14,7 @@
#include <tuple>
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/dcheck_is_on.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
@ -1831,7 +1832,7 @@ bool Database::OpenInternal(const std::string& db_file_path,
#if BUILDFLAG(IS_WIN)
if (mode == OpenMode::kNone || mode == OpenMode::kRetryOnPoision) {
// Do not allow query injection.
if (db_file_path.find('?') != std::string::npos) {
if (base::Contains(db_file_path, '?')) {
return false;
}
open_flags |= SQLITE_OPEN_URI;

@ -8,6 +8,7 @@
#include <stdint.h>
#include <cstdint>
#include "base/containers/contains.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
@ -1592,8 +1593,8 @@ TEST_P(SQLDatabaseTest, OnMemoryDump) {
// worrying too much about what they generate (since that will change).
TEST_P(SQLDatabaseTest, CollectDiagnosticInfo) {
const std::string corruption_info = db_->CollectCorruptionInfo();
EXPECT_NE(std::string::npos, corruption_info.find("SQLITE_CORRUPT"));
EXPECT_NE(std::string::npos, corruption_info.find("integrity_check"));
EXPECT_TRUE(base::Contains(corruption_info, "SQLITE_CORRUPT"));
EXPECT_TRUE(base::Contains(corruption_info, "integrity_check"));
// A statement to see in the results.
const char* kSimpleSql = "SELECT 'mountain'";
@ -1604,7 +1605,7 @@ TEST_P(SQLDatabaseTest, CollectDiagnosticInfo) {
DatabaseDiagnostics diagnostics;
const std::string readonly_info =
db_->CollectErrorInfo(SQLITE_READONLY, &s, &diagnostics);
EXPECT_NE(std::string::npos, readonly_info.find(kSimpleSql));
EXPECT_TRUE(base::Contains(readonly_info, kSimpleSql));
EXPECT_EQ(diagnostics.sql_statement, kSimpleSql);
}
@ -1613,7 +1614,7 @@ TEST_P(SQLDatabaseTest, CollectDiagnosticInfo) {
DatabaseDiagnostics diagnostics;
const std::string full_info =
db_->CollectErrorInfo(SQLITE_FULL, nullptr, &diagnostics);
EXPECT_EQ(std::string::npos, full_info.find(kSimpleSql));
EXPECT_FALSE(base::Contains(full_info, kSimpleSql));
EXPECT_TRUE(diagnostics.sql_statement.empty());
}
@ -1628,9 +1629,9 @@ TEST_P(SQLDatabaseTest, CollectDiagnosticInfo) {
DatabaseDiagnostics diagnostics;
const std::string error_info =
db_->CollectErrorInfo(SQLITE_ERROR, &s, &diagnostics);
EXPECT_NE(std::string::npos, error_info.find(kSimpleSql));
EXPECT_NE(std::string::npos, error_info.find("volcano"));
EXPECT_NE(std::string::npos, error_info.find("version: 4"));
EXPECT_TRUE(base::Contains(error_info, kSimpleSql));
EXPECT_TRUE(base::Contains(error_info, "volcano"));
EXPECT_TRUE(base::Contains(error_info, "version: 4"));
EXPECT_EQ(diagnostics.sql_statement, kSimpleSql);
EXPECT_EQ(diagnostics.version, 4);
@ -1656,15 +1657,14 @@ TEST_P(SQLDatabaseTest, CollectDiagnosticInfo) {
const std::string error_info =
db_->CollectErrorInfo(SQLITE_ERROR, &s, &diagnostics);
// Expect that the error message contains the table name and a column error.
EXPECT_NE(diagnostics.error_message.find("table"), std::string::npos);
EXPECT_NE(diagnostics.error_message.find("volcano"), std::string::npos);
EXPECT_NE(diagnostics.error_message.find("column"), std::string::npos);
EXPECT_TRUE(base::Contains(diagnostics.error_message, "table"));
EXPECT_TRUE(base::Contains(diagnostics.error_message, "volcano"));
EXPECT_TRUE(base::Contains(diagnostics.error_message, "column"));
// Expect that bound values are not present.
EXPECT_EQ(diagnostics.error_message.find("bound_value1"),
std::string::npos);
EXPECT_EQ(diagnostics.error_message.find("42"), std::string::npos);
EXPECT_EQ(diagnostics.error_message.find("1234"), std::string::npos);
EXPECT_FALSE(base::Contains(diagnostics.error_message, "bound_value1"));
EXPECT_FALSE(base::Contains(diagnostics.error_message, "42"));
EXPECT_FALSE(base::Contains(diagnostics.error_message, "1234"));
}
}

@ -13,6 +13,7 @@
#include <vector>
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/dcheck_is_on.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
@ -677,18 +678,18 @@ bool Recovery::AutoRecoverTable(const char* table_name,
// somehow use data which would allow detecting the various type
// coercions which happen. If STRICT could be enabled, type
// mismatches could be detected by which rows are filtered.
if (column_type.find("INT") != std::string::npos) {
if (base::Contains(column_type, "INT")) {
if (pk_column == 1) {
rowid_ofs = create_column_decls.size();
rowid_decl = column_name + " ROWID";
}
column_decl += " INTEGER";
} else if (column_type.find("CHAR") != std::string::npos ||
column_type.find("TEXT") != std::string::npos) {
} else if (base::Contains(column_type, "CHAR") ||
base::Contains(column_type, "TEXT")) {
column_decl += " TEXT";
} else if (column_type == "BLOB") {
column_decl += " BLOB";
} else if (column_type.find("DOUB") != std::string::npos) {
} else if (base::Contains(column_type, "DOUB")) {
column_decl += " FLOAT";
} else {
// TODO(shess): AFAICT, there remain:

@ -5,6 +5,7 @@
#include <limits>
#include <string>
#include "base/containers/contains.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
@ -349,9 +350,10 @@ TEST_F(StatementTest, GetSQLStatementExcludesBoundValues) {
ASSERT_TRUE(insert.Run());
// Verify that GetSQLStatement doesn't leak any bound values that may be PII.
EXPECT_EQ(insert.GetSQLStatement(), "INSERT INTO texts(t) VALUES(?)");
EXPECT_EQ(insert.GetSQLStatement().find("VALUES"), 21U);
EXPECT_EQ(insert.GetSQLStatement().find("Doe"), std::string::npos);
std::string sql_statement = insert.GetSQLStatement();
EXPECT_TRUE(base::Contains(sql_statement, "INSERT INTO texts(t) VALUES(?)"));
EXPECT_TRUE(base::Contains(sql_statement, "VALUES"));
EXPECT_FALSE(base::Contains(sql_statement, "Doe"));
// Sanity check that the name was actually committed.
Statement select(db_.GetUniqueStatement("SELECT t FROM texts ORDER BY id"));