Skip to content

Commit 8d6d989

Browse files
Fix: Replace hardcoded 256-byte shared memory buffer with 4096-byte SHM_SIZE constant (#514)
The C++ shared memory implementation in concore.hpp and concoredocker.hpp allocated a fixed 256-byte buffer for inter-node data exchange and silently truncated any payload exceeding 255 characters. Changes: - Add static constexpr SHM_SIZE = 4096 in both concore.hpp and concoredocker.hpp - Replace all hardcoded 256 in shmget(), strnlen(), and strncpy() with SHM_SIZE - Add overflow detection: stderr error message when payload exceeds SHM_SIZE - Add explicit null termination after strncpy() to prevent read overruns - Buffer now supports ~200+ double values (up from ~25) Fixes #514
1 parent 91bd9d9 commit 8d6d989

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

concore.hpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class Concore{
4040
string inpath = "./in";
4141
string outpath = "./out";
4242

43+
static constexpr size_t SHM_SIZE = 4096;
44+
4345
int shmId_create = -1;
4446
int shmId_get = -1;
4547

@@ -259,7 +261,7 @@ class Concore{
259261
*/
260262
void createSharedMemory(key_t key)
261263
{
262-
shmId_create = shmget(key, 256, IPC_CREAT | 0666);
264+
shmId_create = shmget(key, SHM_SIZE, IPC_CREAT | 0666);
263265

264266
if (shmId_create == -1) {
265267
std::cerr << "Failed to create shared memory segment." << std::endl;
@@ -284,7 +286,7 @@ class Concore{
284286
const int MAX_RETRY = 100;
285287
while (retry < MAX_RETRY) {
286288
// Get the shared memory segment created by Writer
287-
shmId_get = shmget(key, 256, 0666);
289+
shmId_get = shmget(key, SHM_SIZE, 0666);
288290
// Check if shared memory exists
289291
if (shmId_get != -1) {
290292
break; // Break the loop if shared memory exists
@@ -490,7 +492,7 @@ class Concore{
490492
try {
491493
if (shmId_get != -1) {
492494
if (sharedData_get && sharedData_get[0] != '\0') {
493-
std::string message(sharedData_get, strnlen(sharedData_get, 256));
495+
std::string message(sharedData_get, strnlen(sharedData_get, SHM_SIZE));
494496
ins = message;
495497
}
496498
else
@@ -515,7 +517,7 @@ class Concore{
515517
this_thread::sleep_for(timespan);
516518
try{
517519
if(shmId_get != -1) {
518-
std::string message(sharedData_get, strnlen(sharedData_get, 256));
520+
std::string message(sharedData_get, strnlen(sharedData_get, SHM_SIZE));
519521
ins = message;
520522
retrycount++;
521523
}
@@ -664,7 +666,13 @@ class Concore{
664666
outfile<<val[i]<<',';
665667
outfile<<val[val.size()-1]<<']';
666668
std::string result = outfile.str();
667-
std::strncpy(sharedData_create, result.c_str(), 256 - 1);
669+
if (result.size() >= SHM_SIZE) {
670+
std::cerr << "ERROR: write_SM payload (" << result.size()
671+
<< " bytes) exceeds " << SHM_SIZE - 1
672+
<< "-byte shared memory limit. Data truncated!" << std::endl;
673+
}
674+
std::strncpy(sharedData_create, result.c_str(), SHM_SIZE - 1);
675+
sharedData_create[SHM_SIZE - 1] = '\0';
668676
// simtime must not be mutated here (issue #385).
669677
}
670678
else{
@@ -689,7 +697,13 @@ class Concore{
689697
this_thread::sleep_for(timespan);
690698
try {
691699
if(shmId_create != -1){
692-
std::strncpy(sharedData_create, val.c_str(), 256 - 1);
700+
if (val.size() >= SHM_SIZE) {
701+
std::cerr << "ERROR: write_SM payload (" << val.size()
702+
<< " bytes) exceeds " << SHM_SIZE - 1
703+
<< "-byte shared memory limit. Data truncated!" << std::endl;
704+
}
705+
std::strncpy(sharedData_create, val.c_str(), SHM_SIZE - 1);
706+
sharedData_create[SHM_SIZE - 1] = '\0';
693707
}
694708
else throw 505;
695709
}

concoredocker.hpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
class Concore {
2828
private:
29+
static constexpr size_t SHM_SIZE = 4096;
30+
2931
int shmId_create = -1;
3032
int shmId_get = -1;
3133
char* sharedData_create = nullptr;
@@ -233,7 +235,7 @@ class Concore {
233235

234236
#ifdef __linux__
235237
void createSharedMemory(key_t key) {
236-
shmId_create = shmget(key, 256, IPC_CREAT | 0666);
238+
shmId_create = shmget(key, SHM_SIZE, IPC_CREAT | 0666);
237239
if (shmId_create == -1) {
238240
std::cerr << "Failed to create shared memory segment.\n";
239241
return;
@@ -249,7 +251,7 @@ class Concore {
249251
int retry = 0;
250252
const int MAX_RETRY = 100;
251253
while (retry < MAX_RETRY) {
252-
shmId_get = shmget(key, 256, 0666);
254+
shmId_get = shmget(key, SHM_SIZE, 0666);
253255
if (shmId_get != -1)
254256
break;
255257
std::cout << "Shared memory does not exist. Make sure the writer process is running.\n";
@@ -345,7 +347,7 @@ class Concore {
345347
std::string ins;
346348
try {
347349
if (shmId_get != -1 && sharedData_get && sharedData_get[0] != '\0')
348-
ins = std::string(sharedData_get, strnlen(sharedData_get, 256));
350+
ins = std::string(sharedData_get, strnlen(sharedData_get, SHM_SIZE));
349351
else
350352
throw 505;
351353
} catch (...) {
@@ -359,7 +361,7 @@ class Concore {
359361
std::this_thread::sleep_for(std::chrono::seconds(delay));
360362
try {
361363
if (shmId_get != -1 && sharedData_get) {
362-
ins = std::string(sharedData_get, strnlen(sharedData_get, 256));
364+
ins = std::string(sharedData_get, strnlen(sharedData_get, SHM_SIZE));
363365
retrycount++;
364366
} else {
365367
retrycount++;
@@ -426,7 +428,13 @@ class Concore {
426428
outfile << val[i] << ',';
427429
outfile << val[val.size() - 1] << ']';
428430
std::string result = outfile.str();
429-
std::strncpy(sharedData_create, result.c_str(), 256 - 1);
431+
if (result.size() >= SHM_SIZE) {
432+
std::cerr << "ERROR: write_SM payload (" << result.size()
433+
<< " bytes) exceeds " << SHM_SIZE - 1
434+
<< "-byte shared memory limit. Data truncated!" << std::endl;
435+
}
436+
std::strncpy(sharedData_create, result.c_str(), SHM_SIZE - 1);
437+
sharedData_create[SHM_SIZE - 1] = '\0';
430438
} catch (...) {
431439
std::cerr << "skipping +" << outpath << port << "/" << name << "\n";
432440
}

0 commit comments

Comments
 (0)