Results 1 to 10 of 10

Thread: popen runtime error - sample code c

  1. #1

    Default popen runtime error - sample code c

    I got this off a website. First, I adapted it to my code. Then just did a paste for test. It gives this error right after it prints the directory listing. Can anyone tell me what is the problem?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main( int argc, char *argv[] )
    {
    
      FILE *fp;
      char path[1035];
    
      /* Open the command for reading. */
      fp = popen("/bin/ls /etc/", "r");
      if (fp == NULL) {
        printf("Failed to run command\n" );
        exit(1);
      }
    
      /* Read the output a line at a time - output it. */
      while (fgets(path, sizeof(path), fp) != NULL) {
        printf("%s", path);
      }
    
      /* close */
      pclose(fp);
    
      return 0;
    }
    Code:
    *** Error in `popenbin': free(): invalid next size (normal): 0x0000000001deacb0 ***
    Aborted (core dumped)

  2. #2
    Join Date
    Sep 2012
    Posts
    5,993

    Default Re: popen runtime error - sample code c

    I understand that "works for me" is not really helpful, but without any information about distribution, its version, compiler version, exact compiler options etc there is not much more to say. I do not see anything in code itself that may cause memory corruption.

  3. #3

    Default Re: popen runtime error - sample code c

    Quote Originally Posted by arvidjaar View Post
    I understand that "works for me" is not really helpful, but without any information about distribution, its version, compiler version, exact compiler options etc there is not much more to say. I do not see anything in code itself that may cause memory corruption.
    Code:
    gcc --version
    gcc (SUSE Linux) 7.5.0
    https://paste.opensuse.org/58a0a4de

    // while (fgets(linebuffer, sizeof(linebuffer), FilePtr) != NULL)
    // printf("%s", linebuffer);

    Find the above statement. Comment out, no errors.


    This code is the original which shows no errors.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main( int argc, char *argv[] )
    {
    
      FILE *fp;
      char path[1035];
    
      /* Open the command for reading. */
      fp = popen("/bin/ls /etc/", "r");
      if (fp == NULL) {
        printf("Failed to run command\n" );
        exit(1);
      }
    
      /* Read the output a line at a time - output it. */
      while (fgets(path, sizeof(path), fp) != NULL) {
        printf("%s", path);
      }
    
      /* close */
      pclose(fp);
    
      return 0;
    }

  4. #4
    Join Date
    Sep 2012
    Posts
    5,993

    Default Re: popen runtime error - sample code c

    Quote Originally Posted by lord_valarian View Post
    This code is the original which shows no errors.
    First you said this code shows error. Now you say this code shows no error. So I assume the problem is solved.

  5. #5

    Default Re: popen runtime error - sample code c

    Quote Originally Posted by arvidjaar View Post
    First you said this code shows error. Now you say this code shows no error. So I assume the problem is solved.
    No, the sample code shows no errors.

    When I add it to my code, it shows a runtime error.

    If you comment out the while statement in my code, the code doesn't show command output, but shows no errors.

    Meaning, something about the while statement in my code is causing an error.

    However, the while statement shouldn't have any errors??

    Code:
    while (fgets(linebuffer, sizeof(linebuffer), FilePtr) != NULL)
       printf("%s", linebuffer);
    Code:
    *** Error in `scanvirusbin': free(): invalid next size (normal): 0x00000000009dccb0 ***
    Aborted (core dumped)
    That statement is the source of the runtime error. I have no idea what's the problem with it.

  6. #6
    Join Date
    Jan 2017
    Location
    Nürnberg, Germany
    Posts
    284

    Default Re: popen runtime error - sample code c

    Quote Originally Posted by lord_valarian View Post
    Code:
    *** Error in `popenbin': free(): invalid next size (normal): 0x0000000001deacb0 ***
    Aborted (core dumped)
    Give gdb a try, the GNU debugger. It admittedly has a steep learning curve, but it really lets you inspect things in detail between single-stepping through your source. The kind of insight gained with a debugger is unrivaled (yet often avoided even by professional coders) in my opinion.

    I personally use gdb with a slightly modified GDB dashboard.

  7. #7

    Default Re: popen runtime error - sample code c

    Quote Originally Posted by unix111 View Post
    Give gdb a try, the GNU debugger. It admittedly has a steep learning curve, but it really lets you inspect things in detail between single-stepping through your source. The kind of insight gained with a debugger is unrivaled (yet often avoided even by professional coders) in my opinion.

    I personally use gdb with a slightly modified GDB dashboard.
    Kdbg and Kdevelop I'v downloaded as well. There is valgrind to detect memory leaks.

    I ran valgrind.

    Code:
    #valgrind --leak-check=yes scanvirusbin -v
    ==8839== Invalid write of size 1
    ==8839==    at 0x4C319B0: strcpy (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==8839==    by 0x4015D6: main (in /root/bin/scanvirusbin)
    ==8839==  Address 0x51f89d8 is 0 bytes after a block of size 8 alloc'd
    ==8839==    at 0x4C2E2DF: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==8839==    by 0x4015BB: main (in /root/bin/scanvirusbin)

  8. #8

    Default Re: popen runtime error - sample code c

    The error is at:

    Code:
    pclose(FilePtr);
    FilePtr is reused, but that shouldn't be a problem.

    I seem to have missed pclose(fileptr) statement. I'll need to retest it.

  9. #9

    Default Re: popen runtime error - sample code c

    Valgrind showed an error in the memory allocation. The sizeof function wasn't giving a useful number. Since, I borrowed this code. I missed what was causing the problem.

    It's not sizeof. It's the variable len. I should be able to fix the memory allocation bug.

  10. #10

    Default Re: popen runtime error - sample code c

    Thanks to all. Now, I have new problem.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •