Skip to content

Commit 01c13da

Browse files
zimmerleFelipe Zimmerle
authored and
Felipe Zimmerle
committed
Fix segfault due to invalid memory access on SharedFiles class
Issue #1318
1 parent 87f6b47 commit 01c13da

File tree

2 files changed

+36
-98
lines changed

2 files changed

+36
-98
lines changed

src/utils/shared_files.cc

Lines changed: 33 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,22 @@ namespace utils {
3737

3838
msc_file_handler_t *SharedFiles::find_handler(
3939
const std::string &fileName) {
40-
msc_file_handler_t *current = m_first;
41-
while (current != NULL) {
42-
if (current->file_name == fileName) {
43-
return current;
40+
for (const auto &i: m_handlers) {
41+
if (i.first == fileName) {
42+
return i.second;
4443
}
45-
current = reinterpret_cast<msc_file_handler_t *>(current->next);
4644
}
47-
4845
return NULL;
4946
}
5047

5148

5249
msc_file_handler_t *SharedFiles::add_new_handler(
5350
const std::string &fileName, std::string *error) {
54-
msc_file_handler_t *current = m_first;
5551
int shm_id;
5652
key_t mem_key_structure;
57-
key_t mem_key_file_name;
5853
msc_file_handler_t *new_debug_log;
59-
char *shm_ptr2;
6054
FILE *fp;
55+
bool toBeCreated = true;
6156

6257
fp = fopen(fileName.c_str(), "a");
6358
if (fp == 0) {
@@ -72,81 +67,44 @@ msc_file_handler_t *SharedFiles::add_new_handler(
7267
goto err_mem_key;
7368
}
7469

75-
mem_key_file_name = ftok(fileName.c_str(), 2);
76-
if (mem_key_file_name < 0) {
77-
error->assign("Failed to select key for the shared memory (2): ");
78-
error->append(strerror(errno));
79-
goto err_mem_key;
80-
}
81-
82-
shm_id = shmget(mem_key_structure, sizeof (msc_file_handler_t),
83-
IPC_CREAT | 0666);
70+
shm_id = shmget(mem_key_structure, sizeof (msc_file_handler_t) + fileName.size() + 1,
71+
IPC_CREAT | IPC_EXCL | 0666);
8472
if (shm_id < 0) {
85-
error->assign("Failed to allocate shared memory (1): ");
86-
error->append(strerror(errno));
87-
goto err_shmget1;
73+
shm_id = shmget(mem_key_structure, sizeof (msc_file_handler_t)
74+
+ fileName.size() + 1, IPC_CREAT | 0666);
75+
toBeCreated = false;
76+
if (shm_id < 0) {
77+
error->assign("Failed to allocate shared memory (1): ");
78+
error->append(strerror(errno));
79+
goto err_shmget1;
80+
}
8881
}
8982

9083
new_debug_log = reinterpret_cast<msc_file_handler_t *>(
91-
shmat(shm_id, NULL, 0));
84+
shmat(shm_id, NULL, 0));
9285
if ((reinterpret_cast<char *>(new_debug_log)[0]) == -1) {
9386
error->assign("Failed to attach shared memory (1): ");
9487
error->append(strerror(errno));
9588
goto err_shmat1;
9689
}
97-
memset(new_debug_log, '\0', sizeof(msc_file_handler_t));
98-
99-
pthread_mutex_init(&new_debug_log->lock, NULL);
100-
new_debug_log->fp = fp;
101-
new_debug_log->file_handler = fileno(new_debug_log->fp);
102-
new_debug_log->next = NULL;
103-
new_debug_log->previous = NULL;
104-
new_debug_log->shm_id_structure = shm_id;
105-
shm_id = shmget(mem_key_file_name, (fileName.size() + 1 * sizeof(char)),
106-
IPC_CREAT | 0666);
107-
if (shm_id < 0) {
108-
error->assign("Failed to allocate shared memory (2): ");
109-
error->append(strerror(errno));
110-
goto err_shmget2;
111-
}
112-
new_debug_log->shm_id_file_name = shm_id;
113-
shm_ptr2 = reinterpret_cast<char *>(shmat(shm_id, NULL, 0));
114-
if (shm_ptr2[0] == -1) {
115-
error->assign("Failed to attach shared memory (2): ");
116-
error->append(strerror(errno));
117-
goto err_shmat2;
118-
}
119-
memcpy(shm_ptr2, fileName.c_str(), fileName.size());
120-
shm_ptr2[fileName.size()] = '\0';
121-
122-
new_debug_log->file_name = shm_ptr2;
123-
124-
if (m_first == NULL) {
125-
m_first = new_debug_log;
126-
} else {
127-
current = m_first;
128-
while (current != NULL) {
129-
if (current->next == NULL) {
130-
current->next = new_debug_log;
131-
new_debug_log->previous = current;
132-
new_debug_log->next = NULL;
133-
current = NULL;
134-
} else {
135-
current = reinterpret_cast<msc_file_handler_t *>(
136-
current->next);
137-
}
138-
}
90+
91+
if (toBeCreated) {
92+
memset(new_debug_log, '\0', sizeof(msc_file_handler_t));
93+
pthread_mutex_init(&new_debug_log->lock, NULL);
94+
new_debug_log->fp = fp;
95+
new_debug_log->file_handler = fileno(new_debug_log->fp);
96+
new_debug_log->shm_id_structure = shm_id;
97+
memcpy(new_debug_log->file_name, fileName.c_str(), fileName.size());
98+
new_debug_log->file_name[fileName.size()] = '\0';
13999
}
100+
m_handlers.push_back(std::make_pair(fileName, new_debug_log));
140101

141102
return new_debug_log;
142-
err_shmget2:
143-
err_shmat2:
144-
shmdt(shm_ptr2);
145-
fclose(new_debug_log->fp);
146103
err_shmget1:
147104
err_shmat1:
148105
shmdt(new_debug_log);
149106
err_mem_key:
107+
fclose(fp);
150108
err_fh:
151109
return NULL;
152110
}
@@ -173,6 +131,7 @@ bool SharedFiles::open(const std::string& fileName, std::string *error) {
173131

174132
void SharedFiles::close(const std::string& fileName) {
175133
msc_file_handler_t *a;
134+
int j = 0;
176135

177136
if (fileName.empty()) {
178137
return;
@@ -186,41 +145,23 @@ void SharedFiles::close(const std::string& fileName) {
186145
a->using_it--;
187146

188147
if (a->using_it == 0) {
189-
bool first = false;
190148
int shm_id1 = a->shm_id_structure;
191-
int shm_id2 = a->shm_id_file_name;
192149
msc_file_handler_t *p , *n;
193150
pthread_mutex_lock(&a->lock);
194151
fclose(a->fp);
195-
196-
p = reinterpret_cast<msc_file_handler_t *>(a->previous);
197-
n = reinterpret_cast<msc_file_handler_t *>(a->next);
198-
if (p != NULL) {
199-
p->next = reinterpret_cast<msc_file_handler_t *>(n);
200-
}
201-
if (n != NULL) {
202-
n->previous = reinterpret_cast<msc_file_handler_t *>(p);
203-
}
204-
a->previous = NULL;
205-
a->next = NULL;
206152
pthread_mutex_unlock(&a->lock);
207153
pthread_mutex_destroy(&a->lock);
208-
209-
if (a->file_name == m_first->file_name) {
210-
first = true;
211-
}
212-
213-
shmdt(a->file_name);
214154
shmdt(a);
215-
216155
shmctl(shm_id1, IPC_RMID, NULL);
217-
shmctl(shm_id2, IPC_RMID, NULL);
156+
}
218157

219-
if (first) {
220-
m_first = NULL;
158+
for (const auto &i: m_handlers) {
159+
if (i.first == fileName) {
160+
j++;
221161
}
222-
a = NULL;
223162
}
163+
164+
m_handlers.erase(m_handlers.begin() + j, m_handlers.begin() + j + 1);
224165
}
225166

226167

src/utils/shared_files.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,12 @@ namespace utils {
3333

3434

3535
typedef struct msc_file_handler {
36-
char *file_name;
3736
FILE *fp;
3837
int file_handler;
39-
int shm_id_file_name;
4038
int shm_id_structure;
4139
int using_it;
4240
pthread_mutex_t lock;
43-
void *next;
44-
void *previous;
41+
char file_name[];
4542
} msc_file_handler_t;
4643

4744

@@ -62,7 +59,7 @@ class SharedFiles {
6259
std::string *error);
6360

6461
private:
65-
SharedFiles() : m_first(NULL) { }
62+
SharedFiles() { }
6663
~SharedFiles() { }
6764

6865
// C++ 03
@@ -73,7 +70,7 @@ class SharedFiles {
7370
SharedFiles(SharedFiles const&);
7471
void operator=(SharedFiles const&);
7572

76-
msc_file_handler_t *m_first;
73+
std::vector<std::pair<std::string, msc_file_handler *>> m_handlers;
7774
};
7875

7976

0 commit comments

Comments
 (0)