Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 20 additions & 6 deletions concore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Concore{
string inpath = "./in";
string outpath = "./out";

static constexpr size_t SHM_SIZE = 4096;

int shmId_create = -1;
int shmId_get = -1;

Expand Down Expand Up @@ -259,7 +261,7 @@ class Concore{
*/
void createSharedMemory(key_t key)
{
shmId_create = shmget(key, 256, IPC_CREAT | 0666);
shmId_create = shmget(key, SHM_SIZE, IPC_CREAT | 0666);

if (shmId_create == -1) {
Comment thread
GaneshPatil7517 marked this conversation as resolved.
std::cerr << "Failed to create shared memory segment." << std::endl;
Expand All @@ -284,7 +286,7 @@ class Concore{
const int MAX_RETRY = 100;
while (retry < MAX_RETRY) {
// Get the shared memory segment created by Writer
shmId_get = shmget(key, 256, 0666);
shmId_get = shmget(key, SHM_SIZE, 0666);
// Check if shared memory exists
if (shmId_get != -1) {
break; // Break the loop if shared memory exists
Expand Down Expand Up @@ -490,7 +492,7 @@ class Concore{
try {
if (shmId_get != -1) {
if (sharedData_get && sharedData_get[0] != '\0') {
std::string message(sharedData_get, strnlen(sharedData_get, 256));
std::string message(sharedData_get, strnlen(sharedData_get, SHM_SIZE));
ins = message;
}
else
Expand All @@ -515,7 +517,7 @@ class Concore{
this_thread::sleep_for(timespan);
try{
if(shmId_get != -1) {
std::string message(sharedData_get, strnlen(sharedData_get, 256));
std::string message(sharedData_get, strnlen(sharedData_get, SHM_SIZE));
ins = message;
retrycount++;
}
Expand Down Expand Up @@ -664,7 +666,13 @@ class Concore{
outfile<<val[i]<<',';
outfile<<val[val.size()-1]<<']';
std::string result = outfile.str();
std::strncpy(sharedData_create, result.c_str(), 256 - 1);
if (result.size() >= SHM_SIZE) {
std::cerr << "ERROR: write_SM payload (" << result.size()
<< " bytes) exceeds " << SHM_SIZE - 1
<< "-byte shared memory limit. Data truncated!" << std::endl;
}
std::strncpy(sharedData_create, result.c_str(), SHM_SIZE - 1);
sharedData_create[SHM_SIZE - 1] = '\0';
// simtime must not be mutated here (issue #385).
Comment thread
GaneshPatil7517 marked this conversation as resolved.
}
else{
Expand All @@ -689,7 +697,13 @@ class Concore{
this_thread::sleep_for(timespan);
try {
if(shmId_create != -1){
std::strncpy(sharedData_create, val.c_str(), 256 - 1);
if (val.size() >= SHM_SIZE) {
std::cerr << "ERROR: write_SM payload (" << val.size()
<< " bytes) exceeds " << SHM_SIZE - 1
<< "-byte shared memory limit. Data truncated!" << std::endl;
}
std::strncpy(sharedData_create, val.c_str(), SHM_SIZE - 1);
sharedData_create[SHM_SIZE - 1] = '\0';
}
Comment thread
GaneshPatil7517 marked this conversation as resolved.
else throw 505;
}
Expand Down
18 changes: 13 additions & 5 deletions concoredocker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

class Concore {
private:
static constexpr size_t SHM_SIZE = 4096;

int shmId_create = -1;
int shmId_get = -1;
char* sharedData_create = nullptr;
Expand Down Expand Up @@ -233,7 +235,7 @@ class Concore {

#ifdef __linux__
void createSharedMemory(key_t key) {
shmId_create = shmget(key, 256, IPC_CREAT | 0666);
shmId_create = shmget(key, SHM_SIZE, IPC_CREAT | 0666);
if (shmId_create == -1) {
std::cerr << "Failed to create shared memory segment.\n";
return;
Expand All @@ -249,7 +251,7 @@ class Concore {
int retry = 0;
const int MAX_RETRY = 100;
while (retry < MAX_RETRY) {
shmId_get = shmget(key, 256, 0666);
shmId_get = shmget(key, SHM_SIZE, 0666);
if (shmId_get != -1)
break;
std::cout << "Shared memory does not exist. Make sure the writer process is running.\n";
Expand Down Expand Up @@ -345,7 +347,7 @@ class Concore {
std::string ins;
try {
if (shmId_get != -1 && sharedData_get && sharedData_get[0] != '\0')
ins = std::string(sharedData_get, strnlen(sharedData_get, 256));
ins = std::string(sharedData_get, strnlen(sharedData_get, SHM_SIZE));
else
throw 505;
} catch (...) {
Expand All @@ -359,7 +361,7 @@ class Concore {
std::this_thread::sleep_for(std::chrono::seconds(delay));
try {
if (shmId_get != -1 && sharedData_get) {
ins = std::string(sharedData_get, strnlen(sharedData_get, 256));
ins = std::string(sharedData_get, strnlen(sharedData_get, SHM_SIZE));
retrycount++;
} else {
retrycount++;
Expand Down Expand Up @@ -426,7 +428,13 @@ class Concore {
outfile << val[i] << ',';
outfile << val[val.size() - 1] << ']';
std::string result = outfile.str();
std::strncpy(sharedData_create, result.c_str(), 256 - 1);
if (result.size() >= SHM_SIZE) {
std::cerr << "ERROR: write_SM payload (" << result.size()
<< " bytes) exceeds " << SHM_SIZE - 1
<< "-byte shared memory limit. Data truncated!" << std::endl;
}
Comment thread
GaneshPatil7517 marked this conversation as resolved.
std::strncpy(sharedData_create, result.c_str(), SHM_SIZE - 1);
sharedData_create[SHM_SIZE - 1] = '\0';
} catch (...) {
std::cerr << "skipping +" << outpath << port << "/" << name << "\n";
}
Expand Down
Loading