Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: read acl file crash on Mac OS #2310

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/acl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

#include <fmt/format.h>
#include <cstring>
#include <fstream>
#include <shared_mutex>

#include "include/acl.h"
#include "include/pika_cmd_table_manager.h"
#include "include/pika_server.h"
#include "pstd_defer.h"
#include "pstd_hash.h"

extern PikaServer* g_pika_server;
Expand Down Expand Up @@ -370,6 +372,8 @@ pstd::Status Acl::LoadUserConfigured(std::vector<std::string>& users) {
}

pstd::Status Acl::LoadUserFromFile(std::set<std::string>* toUnAuthUsers) {
std::unique_lock wl(mutex_);

for (const auto& item : users_) {
if (item.first != DefaultUser) {
toUnAuthUsers->insert(item.first);
Expand All @@ -389,30 +393,27 @@ pstd::Status Acl::LoadUserFromFile(const std::string& fileName) {
return pstd::Status::OK();
}

std::unique_lock wl(mutex_);

std::unique_ptr<pstd::SequentialFile> sequentialFile;
auto status = NewSequentialFile(fileName, sequentialFile);
if (!status.ok()) {
return status;
}

std::map<std::string, std::shared_ptr<User>> users;
std::vector<std::string> rules;
const int lineLength = 1024 * 1024;
char line[lineLength];

bool hasDefaultUser = false;

std::ifstream ruleFile(fileName);
if (!ruleFile) {
return pstd::Status::IOError(fmt::format("open file {} fail"), fileName);
}

DEFER { ruleFile.close(); };

int lineNum = 0;
while (sequentialFile->ReadLine(line, lineLength) != nullptr) {
std::string lineContent;
while (std::getline(ruleFile, lineContent)) {
++lineNum;
size_t lineLen = strlen(line);
if (lineLen == 0) {
if (lineContent.empty()) {
continue;
}

std::string lineContent = pstd::StringTrim(line, "\r\n ");
lineContent = pstd::StringTrim(lineContent, "\r\n ");
rules.clear();
pstd::StringSplit(lineContent, ' ', rules);
if (rules.empty()) {
Expand All @@ -438,7 +439,7 @@ pstd::Status Acl::LoadUserFromFile(const std::string& fileName) {

auto u = CreatedUser(rules[1]);
for (const auto& item : aclArgc) {
status = u->SetUser(item);
auto status = u->SetUser(item);
if (!status.ok()) {
LOG(ERROR) << "load user from acl file error," << status.ToString();
return status;
Expand Down
Loading