title: Program crashing in munmap
body: <p>I am working at an Android app that, among other things, must send background data to a ftp server. The code that does this is written in native code, using standard Linux functions.</p>
  <p>Most of the time it works fine, but every once in a while it crashes, and the crash drives me crazy, because it makes no sense to me.</p>
  <p>Here is the relevant code:</p>
  <pre><code>
   if(!sbuf.st_size)
   {
       syslog(LOG_CRIT, &quot;FTP: OMFG WE GOT 0 FILE SIZE!!!11!!!! &quot;);
       close(fd);
       fclose(stream);
       close(dsock);
       return 0;
   }
  
   p = mmap(0, (size_t) sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  
   if(p==MAP_FAILED)
   {
       syslog(LOG_CRIT, &quot;FTP: We got a mmap problem.... %s&quot;,strerror(errno));
       close(fd);
       fclose(stream);
       close(dsock);
       return 0;
   }
  
   syslog(LOG_CRIT, &quot;Before fwrite&quot;);
  
   if(fwrite(p, 1, (size_t) sbuf.st_size, stream)!=(size_t) sbuf.st_size)
   {
       syslog(LOG_CRIT, &quot;FTP: We got a fwrite problem.... %s&quot;,strerror(errno));
       munmap(p, (size_t) sbuf.st_size);
       close(fd);
       fclose(stream);
       close(dsock);
       return 0;
   }
  
   fflush(stream);
   usleep(150000);
   syslog(LOG_CRIT, &quot;Before munmap&quot;);
  
   munmap(p, (size_t) sbuf.st_size);
   //fflush(stream);
   close(fd);
   fclose(stream);
   close(dsock);
  
   int tries=0;
  
   while(1) {
  
       if(tries&gt;3)return 0;
  
       len = ftpTryRead(csock, buffer, 128);
       if (len &lt;= 0) {
           syslog(LOG_CRIT, &quot;FTP: Got null after upload, len is %i&quot;,len);
           //return 0;
           usleep(300000);
           tries++;
           continue;
       }
  
       if(!strncmp(buffer,&quot;226&quot;,3))break;
       else
       {
           syslog(LOG_CRIT, &quot;FTP: Expected 226 but got %s&quot;,buffer);
           return 0;
       }
   }
   //sleep(2);
  
   syslog(LOG_CRIT, &quot;FTP: Uploading of file %s should be completed.&quot;,file);
   unlink(file_name);
   
   return 1;
  </code></pre>
  <p>The relevant stuff in the logcat is this:</p>
  <pre><code>07-13 21:30:50.557 10268-10376/? E/com.example.ftp_cam: Before munmap
  07-13 21:30:50.561 10268-15934/? E/IMemory: cannot dup fd=69, size=4096, err=0 (Bad file descriptor)
  07-13 21:30:50.561 10268-15934/? E/IMemory: cannot map BpMemoryHeap (binder=0x7f57d239a0), size=4096, fd=-1 (Bad file descriptor)
  07-13 21:30:50.561 10268-15934/? A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 15934 (Binder:10268_6)
  07-13 21:30:50.570 10268-10376/? E/com.example.ftp_cam: FTP: Uploading of file IMG_20200713_082444.jpg should be completed.
  </code></pre>
  <p>So it seems that the code crashes in munmap. However, the code continues for a bit after the segfault, because it shows the file upload completed message (which is further down the code).</p>
  <p>Any idea what might the problem be?
  Thanks for your time!</p>