title: Run-Time Check Failure #2 - Stack around the variable --- was corrupted
body: <p>When I run This program I get the following error:
  Run-Time Check Failure #2 - Stack around the variable 'out_result' was corrupted.
  I tried to print the length of out_result to check if there was an overrun to the buffer, but the length is always less than CHAR_MAX_LENGTH =512
  Any ideas?</p>
  
  <pre><code>void GetCPUOutResult(FILE* cpu_out_file_pointer,char* out_result)
  {
  char temp[CHAR_MAX_LENGTH+1] = {0};
  int char_index =fgetc(cpu_out_file_pointer) ,index=0;
  
  while ( ((char)char_index) != '\n')
  {
          temp[index]=(char)char_index;
          char_index =fgetc(cpu_out_file_pointer);
          index++;
  }
  
  temp[index]='\0';
  sprintf_s(  out_result, 
              MAX_CMD_LINE_LENGTH, 
              temp, 
              NULL); 
  }
  
  
  
  void WorkerThreadFunction(THREAD_PARAMETERS* parameters)//LPVOID lpParam
  {
  
  //THREAD_PARAMETERS* parameters = (THREAD_PARAMETERS *) lpParam;
  char *mission;
  char cpu_file_name_in[CHAR_MAX_LENGTH];
  char cpu_file_name_out[CHAR_MAX_LENGTH];
  char out_result[CHAR_MAX_LENGTH+1]={0};
  FILE *cpu_in_file_pointer, *cpu_out_file_pointer;
  HANDLE mutexes_handles[NUM_MUTEXES];
  int thread_id=parameters-&gt;thread_id;
  char *log_file_name=parameters-&gt;log_file_name;
  HANDLE youHaveWorkToDo=parameters-&gt;youHaveWorkToDoSemaphore[thread_id];
  HANDLE readyToAcceptNewWork=parameters-&gt;readyToAcceptNewWorkSemaphore[thread_id];
  
  
  //open file cpu&lt;threadID&gt;.in for writing
  
  cpu_file_name_in[ 0 ] = '\0';
  GenerateCPUFileName(cpu_file_name_in,"in",thread_id);
  cpu_in_file_pointer = _fsopen(cpu_file_name_in, "w", _SH_DENYNO);
  
  
  //open file cpu&lt;id&gt;.out for reading
  cpu_file_name_out[ 0 ] = '\0';
  GenerateCPUFileName(cpu_file_name_out,"out",thread_id);
  cpu_out_file_pointer = _fsopen( cpu_file_name_out, "r", _SH_DENYNO );
  
  while (TRUE)
  {
      printf("Worker Thread: %d is waiting for work from dispatcher\n",thread_id);
      WaitForDispatcherToGiveMeWorkToDo(youHaveWorkToDo);
      THREADS_STATUS_ARRAY[thread_id]=0;/////////////////////////////////////////////////////////////////
      printf("Worker Thread: %d GOT work from dispatcher\n",thread_id);
  
  
      mission= WORKER_THREADS_BUFFER[thread_id];
      printf("Worker Thread: %d is executing mission %s\n",thread_id,mission);
  
  
      //pre mission execution writing to log file with lock
  
      mutexes_handles[0] = AquireSingleInstanceLock(FIRST_INSTANCE_MUTEX_NAME);//Set LOCK
      printf("Worker Thread: %d is trying to witre to INPUT LOGFILE\n",thread_id);
      WriteToLogFile(log_file_name,
                      thread_id,
                      " started running mission ",
                      mission);
      printf("Worker Thread: %d wrote successfully into INPUT LOGFILE\n",thread_id);
      ReleaseMutex( mutexes_handles[0] );//free LOCK
  
  
      //Write mission from worker thread buffer into cpu&lt;id&gt;.in
      sprintf(mission,"%s\n",mission);
      fwrite( mission, sizeof( char ), strlen(mission), cpu_in_file_pointer);
      fflush( cpu_in_file_pointer );//flush data from accoiated buffer to the file
      printf("Worker Thread: %d wrote successfully in cpu%d.in core\n",thread_id,thread_id);
  
  
      //get result from cpu&lt;id&gt;.out
      printf("Worker Thread: %d is trying to get result from CPU&lt;Id&gt;.OUT\n",thread_id);
  
  
      Sleep(50);
      GetCPUOutResult(cpu_out_file_pointer,out_result);
      printf("Worker Thread: %d got result:%s from CPU&lt;Id&gt;.OUT\n result length is:%d\n",thread_id,out_result,strlen(out_result));
  
      ///post mission execution writing to log file with lock
      printf("Worker Thread: %d trying to write result:%s into OUT LOG FILE\n",thread_id,out_result);
      mutexes_handles[1] = AquireSingleInstanceLock(SECOND_INSTANCE_MUTEX_NAME);//Set LOCK
      WriteToLogFile(log_file_name,thread_id," is done! Mission result is ",out_result);
      ReleaseMutex( mutexes_handles[0] );//free LOCK
      printf("Worker Thread: %d successfully wrote result:%s into OUT LOG FILE\n",thread_id,out_result);
  
      printf("Worker Thread: %d successfully executed mission:%s \n",thread_id,mission);
  
      if (strcmp(mission,"quit\n") == 0  )
      {
          break;  
      }
      else
      {
          InformDispatcherThreadIsReadyToAcceptWork(readyToAcceptNewWork,
                                                (LPLONG)&amp;THREADS_STATUS_ARRAY[thread_id]);
          THREADS_STATUS_ARRAY[thread_id]=1;/////////////////////////////////////////////////////////////////
      }
  }
  return;
  
  }
  </code></pre>