Skip to content

Commit 78abaa8

Browse files
committed
fix(rime_api.cc): dangling pointer returned from RimeGetSharedDataDir
introduced in commit 4b84dbf
1 parent b40c8b4 commit 78abaa8

9 files changed

+47
-42
lines changed

src/rime/deployer.cc

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <chrono>
88
#include <utility>
99
#include <boost/date_time/posix_time/posix_time_types.hpp>
10+
#include <boost/filesystem.hpp>
1011
#include <rime/deployer.h>
1112

1213
namespace rime {
@@ -136,4 +137,8 @@ void Deployer::JoinMaintenanceThread() {
136137
JoinWorkThread();
137138
}
138139

140+
string Deployer::user_data_sync_dir() const {
141+
return (boost::filesystem::path(sync_dir) / user_id).string();
142+
}
143+
139144
} // namespace rime

src/rime/deployer.h

+6-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <mutex>
1212
#include <queue>
1313
#include <boost/any.hpp>
14-
#include <boost/filesystem.hpp>
1514
#include <rime/common.h>
1615
#include <rime/component.h>
1716
#include <rime/messenger.h>
@@ -33,11 +32,11 @@ class DeploymentTask : public Class<DeploymentTask, TaskInitializer> {
3332
class Deployer : public Messenger {
3433
public:
3534
// read-only access after library initialization {
36-
boost::filesystem::path shared_data_dir;
37-
boost::filesystem::path user_data_dir;
38-
boost::filesystem::path prebuilt_data_dir;
39-
boost::filesystem::path staging_dir;
40-
boost::filesystem::path sync_dir;
35+
string shared_data_dir;
36+
string user_data_dir;
37+
string prebuilt_data_dir;
38+
string staging_dir;
39+
string sync_dir;
4140
string user_id;
4241
string distribution_name;
4342
string distribution_code_name;
@@ -64,9 +63,7 @@ class Deployer : public Messenger {
6463
void JoinWorkThread();
6564
void JoinMaintenanceThread();
6665

67-
boost::filesystem::path user_data_sync_dir() const {
68-
return sync_dir / user_id;
69-
}
66+
string user_data_sync_dir() const;
7067

7168
private:
7269
std::queue<of<DeploymentTask>> pending_tasks_;

src/rime/lever/custom_settings.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ CustomSettings::CustomSettings(Deployer* deployer,
3333
}
3434

3535
bool CustomSettings::Load() {
36-
fs::path config_path = deployer_->staging_dir / (config_id_ + ".yaml");
36+
fs::path config_path = fs::path(deployer_->staging_dir) / (config_id_ + ".yaml");
3737
if (!config_.LoadFromFile(config_path.string())) {
38-
config_path = deployer_->prebuilt_data_dir / (config_id_ + ".yaml");
38+
config_path = fs::path(deployer_->prebuilt_data_dir) / (config_id_ + ".yaml");
3939
if (!config_.LoadFromFile(config_path.string())) {
4040
LOG(WARNING) << "cannot find '" << config_id_ << ".yaml'.";
4141
}
4242
}
4343
fs::path custom_config_path =
44-
deployer_->user_data_dir / custom_config_file(config_id_);
44+
fs::path(deployer_->user_data_dir) / custom_config_file(config_id_);
4545
if (!custom_config_.LoadFromFile(custom_config_path.string())) {
4646
return false;
4747
}

src/rime/lever/deployment_tasks.cc

+12-12
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ bool DetectModifications::Run(Deployer* deployer) {
7878

7979
bool InstallationUpdate::Run(Deployer* deployer) {
8080
LOG(INFO) << "updating rime installation info.";
81-
const fs::path& shared_data_path(deployer->shared_data_dir);
82-
const fs::path& user_data_path(deployer->user_data_dir);
81+
const fs::path shared_data_path(deployer->shared_data_dir);
82+
const fs::path user_data_path(deployer->user_data_dir);
8383
if (!fs::exists(user_data_path)) {
8484
LOG(INFO) << "creating user data dir: " << user_data_path.string();
8585
boost::system::error_code ec;
@@ -104,7 +104,7 @@ bool InstallationUpdate::Run(Deployer* deployer) {
104104
if (config.GetString("sync_dir", &sync_dir)) {
105105
deployer->sync_dir = sync_dir;
106106
} else {
107-
deployer->sync_dir = user_data_path / "sync";
107+
deployer->sync_dir = (fs::path(user_data_path) / "sync").string();
108108
}
109109
LOG(INFO) << "sync dir: " << deployer->sync_dir;
110110
if (config.GetString("distribution_code_name", &last_distro_code_name)) {
@@ -359,7 +359,7 @@ bool SchemaUpdate::Run(Deployer* deployer) {
359359
}
360360

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

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

457457
bool PrebuildAllSchemas::Run(Deployer* deployer) {
458-
const fs::path& shared_data_path(deployer->shared_data_dir);
459-
const fs::path& user_data_path(deployer->user_data_dir);
458+
const fs::path shared_data_path(deployer->shared_data_dir);
459+
const fs::path user_data_path(deployer->user_data_dir);
460460
if (!fs::exists(shared_data_path) || !fs::is_directory(shared_data_path))
461461
return false;
462462
bool success = true;
@@ -473,8 +473,8 @@ bool PrebuildAllSchemas::Run(Deployer* deployer) {
473473
}
474474

475475
bool SymlinkingPrebuiltDictionaries::Run(Deployer* deployer) {
476-
const fs::path& shared_data_path(deployer->shared_data_dir);
477-
const fs::path& user_data_path(deployer->user_data_dir);
476+
const fs::path shared_data_path(deployer->shared_data_dir);
477+
const fs::path user_data_path(deployer->user_data_dir);
478478
if (!fs::exists(shared_data_path) || !fs::is_directory(shared_data_path) ||
479479
!fs::exists(user_data_path) || !fs::is_directory(user_data_path) ||
480480
fs::equivalent(shared_data_path, user_data_path))
@@ -545,7 +545,7 @@ static bool IsCustomizedCopy(const string& file_name) {
545545

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

593593
bool CleanupTrash::Run(Deployer* deployer) {
594594
LOG(INFO) << "clean up trash.";
595-
const fs::path& user_data_path(deployer->user_data_dir);
595+
const fs::path user_data_path(deployer->user_data_dir);
596596
if (!fs::exists(user_data_path))
597597
return false;
598598
fs::path trash = user_data_path / "trash";

src/rime/resource.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ResourceResolver {
2929
RIME_API virtual boost::filesystem::path ResolvePath(const string& resource_id);
3030
string ToResourceId(const string& file_path) const;
3131
string ToFilePath(const string& resource_id) const;
32-
void set_root_path(const boost::filesystem::path& root_path) {
32+
void set_root_path(boost::filesystem::path root_path) {
3333
root_path_ = root_path;
3434
}
3535
boost::filesystem::path root_path() const {
@@ -47,7 +47,7 @@ class FallbackResourceResolver : public ResourceResolver {
4747
: ResourceResolver(type) {
4848
}
4949
RIME_API boost::filesystem::path ResolvePath(const string& resource_id) override;
50-
void set_fallback_root_path(const boost::filesystem::path& fallback_root_path) {
50+
void set_fallback_root_path(boost::filesystem::path fallback_root_path) {
5151
fallback_root_path_ = fallback_root_path;
5252
}
5353
private:

src/rime/setup.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
#include <glog/logging.h>
1010
#endif // RIME_ENABLE_LOGGING
1111

12+
#include <boost/filesystem.hpp>
1213
#include <rime_api.h>
1314
#include <rime/deployer.h>
1415
#include <rime/module.h>
1516
#include <rime/service.h>
1617
#include <rime/setup.h>
1718

19+
namespace fs = boost::filesystem;
20+
1821
namespace rime {
1922

2023
#define Q(x) #x
@@ -54,11 +57,11 @@ RIME_API void SetupDeployer(RimeTraits *traits) {
5457
if (PROVIDED(traits, prebuilt_data_dir))
5558
deployer.prebuilt_data_dir = traits->prebuilt_data_dir;
5659
else
57-
deployer.prebuilt_data_dir = deployer.shared_data_dir / "build";
60+
deployer.prebuilt_data_dir = (fs::path(deployer.shared_data_dir) / "build").string();
5861
if (PROVIDED(traits, staging_dir))
5962
deployer.staging_dir = traits->staging_dir;
6063
else
61-
deployer.staging_dir = deployer.user_data_dir / "build";
64+
deployer.staging_dir = (fs::path(deployer.user_data_dir) / "build").string();
6265
}
6366

6467
RIME_API void SetupLogging(const char* app_name, int min_log_level, const char* log_dir) {

src/rime_api.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ RIME_API Bool RimeStartMaintenance(Bool full_check) {
9797
if (!full_check) {
9898
TaskInitializer args{
9999
vector<string>{
100-
deployer.user_data_dir.string(),
101-
deployer.shared_data_dir.string(),
100+
deployer.user_data_dir,
101+
deployer.shared_data_dir,
102102
},
103103
};
104104
if (!deployer.RunTask("detect_modifications", args)) {
@@ -875,27 +875,27 @@ RIME_API Bool RimeRunTask(const char* task_name) {
875875

876876
RIME_API const char* RimeGetSharedDataDir() {
877877
Deployer &deployer(Service::instance().deployer());
878-
return deployer.shared_data_dir.string().c_str();
878+
return deployer.shared_data_dir.c_str();
879879
}
880880

881881
RIME_API const char* RimeGetUserDataDir() {
882882
Deployer &deployer(Service::instance().deployer());
883-
return deployer.user_data_dir.string().c_str();
883+
return deployer.user_data_dir.c_str();
884884
}
885885

886886
RIME_API const char* RimeGetPrebuiltDataDir() {
887887
Deployer &deployer(Service::instance().deployer());
888-
return deployer.prebuilt_data_dir.string().c_str();
888+
return deployer.prebuilt_data_dir.c_str();
889889
}
890890

891891
RIME_API const char* RimeGetStagingDir() {
892892
Deployer &deployer(Service::instance().deployer());
893-
return deployer.staging_dir.string().c_str();
893+
return deployer.staging_dir.c_str();
894894
}
895895

896896
RIME_API const char* RimeGetSyncDir() {
897897
Deployer &deployer(Service::instance().deployer());
898-
return deployer.sync_dir.string().c_str();
898+
return deployer.sync_dir.c_str();
899899
}
900900

901901
RIME_API const char* RimeGetUserId() {
@@ -905,7 +905,7 @@ RIME_API const char* RimeGetUserId() {
905905

906906
RIME_API void RimeGetUserDataSyncDir(char* dir, size_t buffer_size) {
907907
Deployer &deployer(Service::instance().deployer());
908-
strncpy(dir, deployer.user_data_sync_dir().string().c_str(), buffer_size);
908+
strncpy(dir, deployer.user_data_sync_dir().c_str(), buffer_size);
909909
}
910910

911911
RIME_API Bool RimeConfigInit(RimeConfig* config) {

tools/rime_deployer.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
// 2012-07-07 GONG Chen <chen.sst@gmail.com>
66
//
77
#include <iostream>
8+
#include <boost/filesystem.hpp>
89
#include <rime/config.h>
910
#include <rime/deployer.h>
1011
#include <rime/service.h>
1112
#include <rime/setup.h>
1213
#include <rime/lever/deployment_tasks.h>
1314

15+
using namespace fs = boost::filesystem;
16+
1417
using namespace rime;
1518

1619
int add_schema(int count, char* schemas[]) {
@@ -70,9 +73,9 @@ static void setup_deployer(Deployer* deployer,
7073
if (argc > 2) {
7174
deployer->staging_dir = argv[2];
7275
} else {
73-
deployer->staging_dir = deployer->user_data_dir / "build";
76+
deployer->staging_dir = (fs::path(deployer->user_data_dir) / "build").string();
7477
}
75-
deployer->prebuilt_data_dir = deployer->shared_data_dir / "build";
78+
deployer->prebuilt_data_dir = (fs::path(deployer->shared_data_dir) / "build").string();
7679
}
7780

7881
int main(int argc, char* argv[]) {

tools/rime_dict_manager.cc

+1-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ int main(int argc, char *argv[]) {
4242
Config config;
4343
if (config.LoadFromFile("installation.yaml")) {
4444
config.GetString("installation_id", &deployer.user_id);
45-
string sync_dir;
46-
if (config.GetString("sync_dir", &sync_dir)) {
47-
deployer.sync_dir = sync_dir;
48-
}
45+
config.GetString("sync_dir", &deployer.sync_dir);
4946
}
5047
}
5148
UserDictManager mgr(&deployer);

0 commit comments

Comments
 (0)