title: Getting "out-of-bounds" and "variable uninitialized" warnings with mapped shared memory
body: <p>As part of my C application for Linux, I wrote a small library to allow different processes to communicate through shared memory areas.</p>
  
  <p>I am having lots of alerts for potential out-of-bounds pointers and potentially uninitialized variables contained in the memory areas I create this way.</p>
  
  <p>Consider the function I wrote to create a shared memory area:</p>
  
  <pre><code>int LayOutShm (const char *shm_key, size_t memory_size, void ** new_memory)
  {
      int fd_value = 0;
      void * pshm = NULL;
      int return_value = MEM_MGMT_SHM_ERROR;
  
      /* Force new_buffer pointer to NULL just like we initialize return_value to MEM_MGMT_SHM_ERROR */
      *new_memory = NULL;
  
      /* Shared object is created with O_EXCL to prevent two namesake areas from being created*/
      fd_value = shm_open(shm_key, O_CREAT | O_RDWR | O_EXCL , S_IRWXU);
  
      if(fd_value &gt; 0){
          if(ftruncate(fd_value, memory_size) == 0){
              /* Here is where we get the pointer to the created memory area */
              pshm = mmap(NULL, memory_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_value, 0);
              if(pshm != MAP_FAILED){
                  return_value = MEM_MGMT_SHM_OK;
                  memset(pshm,0,memory_size); /* Initialize the memory area */
                  *new_memory = pshm; /* Pass the pointer back to the caller */
              }
          }
      }
  
      return return_value;
  }/*LayOutShm*/
  </code></pre>
  
  <p>Now, this is some piece of code were I get warnings:</p>
  
  <pre><code>#define SHM_KEY                 "my_shm_key"
  
  typedef struct{
      pthread_mutex_t shm_mutex;
      int shm_var1;
      char shm_var2;
      union{
          int shm_union_var1;
          char shm_union_var2;
      }shm_union
  }t_shm_area;
  
  static t_shm_area * shm_area = NULL;
  
  static int ShmInitialization(void)
  {
      int return_value = -1;
  
      (void)LayOutShm(SHM_KEY, sizeof(t_shm_area), (void**)&amp;shm_area);
  
      /*Check for errors in shared memory creation*/
      if (shm_area == NULL){
          syslog(LOG_ERR | LOG_USER, "Error laying out shared memory segment\n");
      }
      else{
          shm_area-&gt;var1 = 0; /* This assignment gets flagged with a potential out-of-bounds runtime error */
          shm_area-&gt;shm_union.shm_union_var2 = 0; /* This assignment gets flagged with a potential out-of-bounds runtime error */
  
          /*Create empty attributes structure*/
          pthread_mutexattr_t mutex_attributes;
  
          /*Initialize attributes structures with default values*/
          (void)pthread_mutexattr_init(&amp;mutex_attributes);
  
          /*Set attributes structure with shared memory value*/
          (void)pthread_mutexattr_setpshared(&amp;mutex_attributes, PTHREAD_PROCESS_SHARED);
  
          /*settype mutex PTHREAD_MUTEX_ERRORCHECK*/
          (void)pthread_mutexattr_settype(&amp;mutex_attributes, PTHREAD_MUTEX_ERRORCHECK);
  
          /*Initialize the mutex with all the previous attributes*/
          (void)pthread_mutex_init(&amp;shm_area-&gt;shm_mutex, &amp;mutex_attributes);
  
          return_value = 0;
      }
  
      return return_value;
  }/*ShmInitialization*/
  </code></pre>
  
  <p>This is the first time I try my hand at double pointers, so I wouldn't be surprised if I were screwing things up with either that void ** in the declaration or the way I cast the input double pointer to void.</p>
  
  <p>A previous implementation of the function returned the pointer directly and didn't generate these problems, but I was asked to change it so that we can return a status code (even if we don't use them for now).</p>
  
  <p>After that area is created, any locally declared variable I pass to the library to get values from the shared memory gets "tainted" with warnings from potential "out-of-bounds" runtime errors or "variable might not be initialized" warnings. The library has already been tested with several different types of data structures (some of them as big as 15kB) and data integrity and performance are satisfying.</p>
  
  <p>Any idea why am I getting these warnings? </p>
  
  <p>Thank you very much, best regards!</p>