Skip to content

Commit

Permalink
fix(rime_api.cc): dangling pointer returned from RimeGetSharedDataDir
Browse files Browse the repository at this point in the history
introduced in commit 4b84dbf
  • Loading branch information
lotem committed Sep 21, 2020
1 parent b40c8b4 commit 78abaa8
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 42 deletions.
5 changes: 5 additions & 0 deletions src/rime/deployer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <chrono>
#include <utility>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/filesystem.hpp>
#include <rime/deployer.h>

namespace rime {
Expand Down Expand Up @@ -136,4 +137,8 @@ void Deployer::JoinMaintenanceThread() {
JoinWorkThread();
}

string Deployer::user_data_sync_dir() const {
return (boost::filesystem::path(sync_dir) / user_id).string();
}

} // namespace rime
15 changes: 6 additions & 9 deletions src/rime/deployer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <mutex>
#include <queue>
#include <boost/any.hpp>
#include <boost/filesystem.hpp>
#include <rime/common.h>
#include <rime/component.h>
#include <rime/messenger.h>
Expand All @@ -33,11 +32,11 @@ class DeploymentTask : public Class<DeploymentTask, TaskInitializer> {
class Deployer : public Messenger {
public:
// read-only access after library initialization {
boost::filesystem::path shared_data_dir;
boost::filesystem::path user_data_dir;
boost::filesystem::path prebuilt_data_dir;
boost::filesystem::path staging_dir;
boost::filesystem::path sync_dir;
string shared_data_dir;
string user_data_dir;
string prebuilt_data_dir;
string staging_dir;
string sync_dir;
string user_id;
string distribution_name;
string distribution_code_name;
Expand All @@ -64,9 +63,7 @@ class Deployer : public Messenger {
void JoinWorkThread();
void JoinMaintenanceThread();

boost::filesystem::path user_data_sync_dir() const {
return sync_dir / user_id;
}
string user_data_sync_dir() const;

private:
std::queue<of<DeploymentTask>> pending_tasks_;
Expand Down
6 changes: 3 additions & 3 deletions src/rime/lever/custom_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ CustomSettings::CustomSettings(Deployer* deployer,
}

bool CustomSettings::Load() {
fs::path config_path = deployer_->staging_dir / (config_id_ + ".yaml");
fs::path config_path = fs::path(deployer_->staging_dir) / (config_id_ + ".yaml");
if (!config_.LoadFromFile(config_path.string())) {
config_path = deployer_->prebuilt_data_dir / (config_id_ + ".yaml");
config_path = fs::path(deployer_->prebuilt_data_dir) / (config_id_ + ".yaml");
if (!config_.LoadFromFile(config_path.string())) {
LOG(WARNING) << "cannot find '" << config_id_ << ".yaml'.";
}
}
fs::path custom_config_path =
deployer_->user_data_dir / custom_config_file(config_id_);
fs::path(deployer_->user_data_dir) / custom_config_file(config_id_);
if (!custom_config_.LoadFromFile(custom_config_path.string())) {
return false;
}
Expand Down
24 changes: 12 additions & 12 deletions src/rime/lever/deployment_tasks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ bool DetectModifications::Run(Deployer* deployer) {

bool InstallationUpdate::Run(Deployer* deployer) {
LOG(INFO) << "updating rime installation info.";
const fs::path& shared_data_path(deployer->shared_data_dir);
const fs::path& user_data_path(deployer->user_data_dir);
const fs::path shared_data_path(deployer->shared_data_dir);
const fs::path user_data_path(deployer->user_data_dir);
if (!fs::exists(user_data_path)) {
LOG(INFO) << "creating user data dir: " << user_data_path.string();
boost::system::error_code ec;
Expand All @@ -104,7 +104,7 @@ bool InstallationUpdate::Run(Deployer* deployer) {
if (config.GetString("sync_dir", &sync_dir)) {
deployer->sync_dir = sync_dir;
} else {
deployer->sync_dir = user_data_path / "sync";
deployer->sync_dir = (fs::path(user_data_path) / "sync").string();
}
LOG(INFO) << "sync dir: " << deployer->sync_dir;
if (config.GetString("distribution_code_name", &last_distro_code_name)) {
Expand Down Expand Up @@ -359,7 +359,7 @@ bool SchemaUpdate::Run(Deployer* deployer) {
}

LOG(INFO) << "preparing dictionary '" << dict_name << "'.";
const fs::path& user_data_path(deployer->user_data_dir);
const fs::path user_data_path(deployer->user_data_dir);
if (!MaybeCreateDirectory(deployer->staging_dir)) {
return false;
}
Expand Down Expand Up @@ -430,8 +430,8 @@ static bool ConfigNeedsUpdate(Config* config) {
}

bool ConfigFileUpdate::Run(Deployer* deployer) {
const fs::path& shared_data_path(deployer->shared_data_dir);
const fs::path& user_data_path(deployer->user_data_dir);
const fs::path shared_data_path(deployer->shared_data_dir);
const fs::path user_data_path(deployer->user_data_dir);
// trash depecated user copy created by an older version of Rime
fs::path source_config_path(shared_data_path / file_name_);
fs::path dest_config_path(user_data_path / file_name_);
Expand All @@ -455,8 +455,8 @@ bool ConfigFileUpdate::Run(Deployer* deployer) {
}

bool PrebuildAllSchemas::Run(Deployer* deployer) {
const fs::path& shared_data_path(deployer->shared_data_dir);
const fs::path& user_data_path(deployer->user_data_dir);
const fs::path shared_data_path(deployer->shared_data_dir);
const fs::path user_data_path(deployer->user_data_dir);
if (!fs::exists(shared_data_path) || !fs::is_directory(shared_data_path))
return false;
bool success = true;
Expand All @@ -473,8 +473,8 @@ bool PrebuildAllSchemas::Run(Deployer* deployer) {
}

bool SymlinkingPrebuiltDictionaries::Run(Deployer* deployer) {
const fs::path& shared_data_path(deployer->shared_data_dir);
const fs::path& user_data_path(deployer->user_data_dir);
const fs::path shared_data_path(deployer->shared_data_dir);
const fs::path user_data_path(deployer->user_data_dir);
if (!fs::exists(shared_data_path) || !fs::is_directory(shared_data_path) ||
!fs::exists(user_data_path) || !fs::is_directory(user_data_path) ||
fs::equivalent(shared_data_path, user_data_path))
Expand Down Expand Up @@ -545,7 +545,7 @@ static bool IsCustomizedCopy(const string& file_name) {

bool BackupConfigFiles::Run(Deployer* deployer) {
LOG(INFO) << "backing up config files.";
const fs::path& user_data_path(deployer->user_data_dir);
const fs::path user_data_path(deployer->user_data_dir);
if (!fs::exists(user_data_path))
return false;
fs::path backup_dir(deployer->user_data_sync_dir());
Expand Down Expand Up @@ -592,7 +592,7 @@ bool BackupConfigFiles::Run(Deployer* deployer) {

bool CleanupTrash::Run(Deployer* deployer) {
LOG(INFO) << "clean up trash.";
const fs::path& user_data_path(deployer->user_data_dir);
const fs::path user_data_path(deployer->user_data_dir);
if (!fs::exists(user_data_path))
return false;
fs::path trash = user_data_path / "trash";
Expand Down
4 changes: 2 additions & 2 deletions src/rime/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ResourceResolver {
RIME_API virtual boost::filesystem::path ResolvePath(const string& resource_id);
string ToResourceId(const string& file_path) const;
string ToFilePath(const string& resource_id) const;
void set_root_path(const boost::filesystem::path& root_path) {
void set_root_path(boost::filesystem::path root_path) {
root_path_ = root_path;
}
boost::filesystem::path root_path() const {
Expand All @@ -47,7 +47,7 @@ class FallbackResourceResolver : public ResourceResolver {
: ResourceResolver(type) {
}
RIME_API boost::filesystem::path ResolvePath(const string& resource_id) override;
void set_fallback_root_path(const boost::filesystem::path& fallback_root_path) {
void set_fallback_root_path(boost::filesystem::path fallback_root_path) {
fallback_root_path_ = fallback_root_path;
}
private:
Expand Down
7 changes: 5 additions & 2 deletions src/rime/setup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
#include <glog/logging.h>
#endif // RIME_ENABLE_LOGGING

#include <boost/filesystem.hpp>
#include <rime_api.h>
#include <rime/deployer.h>
#include <rime/module.h>
#include <rime/service.h>
#include <rime/setup.h>

namespace fs = boost::filesystem;

namespace rime {

#define Q(x) #x
Expand Down Expand Up @@ -54,11 +57,11 @@ RIME_API void SetupDeployer(RimeTraits *traits) {
if (PROVIDED(traits, prebuilt_data_dir))
deployer.prebuilt_data_dir = traits->prebuilt_data_dir;
else
deployer.prebuilt_data_dir = deployer.shared_data_dir / "build";
deployer.prebuilt_data_dir = (fs::path(deployer.shared_data_dir) / "build").string();
if (PROVIDED(traits, staging_dir))
deployer.staging_dir = traits->staging_dir;
else
deployer.staging_dir = deployer.user_data_dir / "build";
deployer.staging_dir = (fs::path(deployer.user_data_dir) / "build").string();
}

RIME_API void SetupLogging(const char* app_name, int min_log_level, const char* log_dir) {
Expand Down
16 changes: 8 additions & 8 deletions src/rime_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ RIME_API Bool RimeStartMaintenance(Bool full_check) {
if (!full_check) {
TaskInitializer args{
vector<string>{
deployer.user_data_dir.string(),
deployer.shared_data_dir.string(),
deployer.user_data_dir,
deployer.shared_data_dir,
},
};
if (!deployer.RunTask("detect_modifications", args)) {
Expand Down Expand Up @@ -875,27 +875,27 @@ RIME_API Bool RimeRunTask(const char* task_name) {

RIME_API const char* RimeGetSharedDataDir() {
Deployer &deployer(Service::instance().deployer());
return deployer.shared_data_dir.string().c_str();
return deployer.shared_data_dir.c_str();
}

RIME_API const char* RimeGetUserDataDir() {
Deployer &deployer(Service::instance().deployer());
return deployer.user_data_dir.string().c_str();
return deployer.user_data_dir.c_str();
}

RIME_API const char* RimeGetPrebuiltDataDir() {
Deployer &deployer(Service::instance().deployer());
return deployer.prebuilt_data_dir.string().c_str();
return deployer.prebuilt_data_dir.c_str();
}

RIME_API const char* RimeGetStagingDir() {
Deployer &deployer(Service::instance().deployer());
return deployer.staging_dir.string().c_str();
return deployer.staging_dir.c_str();
}

RIME_API const char* RimeGetSyncDir() {
Deployer &deployer(Service::instance().deployer());
return deployer.sync_dir.string().c_str();
return deployer.sync_dir.c_str();
}

RIME_API const char* RimeGetUserId() {
Expand All @@ -905,7 +905,7 @@ RIME_API const char* RimeGetUserId() {

RIME_API void RimeGetUserDataSyncDir(char* dir, size_t buffer_size) {
Deployer &deployer(Service::instance().deployer());
strncpy(dir, deployer.user_data_sync_dir().string().c_str(), buffer_size);
strncpy(dir, deployer.user_data_sync_dir().c_str(), buffer_size);
}

RIME_API Bool RimeConfigInit(RimeConfig* config) {
Expand Down
7 changes: 5 additions & 2 deletions tools/rime_deployer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
// 2012-07-07 GONG Chen <chen.sst@gmail.com>
//
#include <iostream>
#include <boost/filesystem.hpp>
#include <rime/config.h>
#include <rime/deployer.h>
#include <rime/service.h>
#include <rime/setup.h>
#include <rime/lever/deployment_tasks.h>

using namespace fs = boost::filesystem;

using namespace rime;

int add_schema(int count, char* schemas[]) {
Expand Down Expand Up @@ -70,9 +73,9 @@ static void setup_deployer(Deployer* deployer,
if (argc > 2) {
deployer->staging_dir = argv[2];
} else {
deployer->staging_dir = deployer->user_data_dir / "build";
deployer->staging_dir = (fs::path(deployer->user_data_dir) / "build").string();
}
deployer->prebuilt_data_dir = deployer->shared_data_dir / "build";
deployer->prebuilt_data_dir = (fs::path(deployer->shared_data_dir) / "build").string();
}

int main(int argc, char* argv[]) {
Expand Down
5 changes: 1 addition & 4 deletions tools/rime_dict_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ int main(int argc, char *argv[]) {
Config config;
if (config.LoadFromFile("installation.yaml")) {
config.GetString("installation_id", &deployer.user_id);
string sync_dir;
if (config.GetString("sync_dir", &sync_dir)) {
deployer.sync_dir = sync_dir;
}
config.GetString("sync_dir", &deployer.sync_dir);
}
}
UserDictManager mgr(&deployer);
Expand Down

0 comments on commit 78abaa8

Please sign in to comment.