@@ -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,10 +261,24 @@ 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;
268+ return ;
269+ }
270+
271+ // Verify the segment is large enough (shmget won't resize an existing segment)
272+ struct shmid_ds shm_info;
273+ if (shmctl (shmId_create, IPC_STAT, &shm_info) == 0 && shm_info.shm_segsz < SHM_SIZE) {
274+ std::cerr << " Shared memory segment too small (" << shm_info.shm_segsz
275+ << " bytes, need " << SHM_SIZE << " ). Removing and recreating." << std::endl;
276+ shmctl (shmId_create, IPC_RMID, nullptr );
277+ shmId_create = shmget (key, SHM_SIZE, IPC_CREAT | 0666 );
278+ if (shmId_create == -1 ) {
279+ std::cerr << " Failed to recreate shared memory segment." << std::endl;
280+ return ;
281+ }
266282 }
267283
268284 // Attach the shared memory segment to the process's address space
@@ -284,7 +300,7 @@ class Concore{
284300 const int MAX_RETRY = 100 ;
285301 while (retry < MAX_RETRY) {
286302 // Get the shared memory segment created by Writer
287- shmId_get = shmget (key, 256 , 0666 );
303+ shmId_get = shmget (key, SHM_SIZE , 0666 );
288304 // Check if shared memory exists
289305 if (shmId_get != -1 ) {
290306 break ; // Break the loop if shared memory exists
@@ -490,7 +506,7 @@ class Concore{
490506 try {
491507 if (shmId_get != -1 ) {
492508 if (sharedData_get && sharedData_get[0 ] != ' \0 ' ) {
493- std::string message (sharedData_get, strnlen (sharedData_get, 256 ));
509+ std::string message (sharedData_get, strnlen (sharedData_get, SHM_SIZE ));
494510 ins = message;
495511 }
496512 else
@@ -515,7 +531,7 @@ class Concore{
515531 this_thread::sleep_for (timespan);
516532 try {
517533 if (shmId_get != -1 ) {
518- std::string message (sharedData_get, strnlen (sharedData_get, 256 ));
534+ std::string message (sharedData_get, strnlen (sharedData_get, SHM_SIZE ));
519535 ins = message;
520536 retrycount++;
521537 }
@@ -658,13 +674,21 @@ class Concore{
658674 try {
659675 std::ostringstream outfile;
660676 if (shmId_create != -1 ){
677+ if (sharedData_create == nullptr )
678+ throw 506 ;
661679 val.insert (val.begin (),simtime+delta);
662680 outfile<<' [' ;
663681 for (int i=0 ;i<val.size ()-1 ;i++)
664682 outfile<<val[i]<<' ,' ;
665683 outfile<<val[val.size ()-1 ]<<' ]' ;
666684 std::string result = outfile.str ();
667- std::strncpy (sharedData_create, result.c_str (), 256 - 1 );
685+ if (result.size () >= SHM_SIZE) {
686+ std::cerr << " ERROR: write_SM payload (" << result.size ()
687+ << " bytes) exceeds " << SHM_SIZE - 1
688+ << " -byte shared memory limit. Data truncated!" << std::endl;
689+ }
690+ std::strncpy (sharedData_create, result.c_str (), SHM_SIZE - 1 );
691+ sharedData_create[SHM_SIZE - 1 ] = ' \0 ' ;
668692 // simtime must not be mutated here (issue #385).
669693 }
670694 else {
@@ -689,7 +713,15 @@ class Concore{
689713 this_thread::sleep_for (timespan);
690714 try {
691715 if (shmId_create != -1 ){
692- std::strncpy (sharedData_create, val.c_str (), 256 - 1 );
716+ if (sharedData_create == nullptr )
717+ throw 506 ;
718+ if (val.size () >= SHM_SIZE) {
719+ std::cerr << " ERROR: write_SM payload (" << val.size ()
720+ << " bytes) exceeds " << SHM_SIZE - 1
721+ << " -byte shared memory limit. Data truncated!" << std::endl;
722+ }
723+ std::strncpy (sharedData_create, val.c_str (), SHM_SIZE - 1 );
724+ sharedData_create[SHM_SIZE - 1 ] = ' \0 ' ;
693725 }
694726 else throw 505 ;
695727 }
0 commit comments