Thread: Error - Inferior 1 exited normally

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    47

    Error - Inferior 1 exited normally

    Hello,

    I'm observing the above error when executing the program.

    Here is an example extract of the relevant code (which works)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    //  Initialise string
        char ip_address[16];
        char subnet[12];
        strcpy(ip_address,"192.168.200.200");
        strcpy(subnet,"");
    
    //  Process string
        int i;
        int period_count = 0;
        char ch;
        int length = strlen(ip_address);
        for(i=0;i<length;i++)
        {
            ch = (ip_address[i]);
    
            if (ch == '.')
            {
                period_count = period_count + 1;
            }
    
            if (period_count == 3)
            {
                break;
            }
    
            strncat(subnet, &ch, 1);
        }
    
        printf("Subnet: %s\n",subnet);
    
        return 0;
    }
    It is difficult for me to post an example of the actual code, without having to post a fair bit of code and some sample input.

    The difference between the above example and the actual code that produces the error is that in the code that fails; the field ip_address is initialised as follows:
    Code:
    strcpy(ip_address,inet_ntoa(source.sin_addr));
    The above statement executes without error, however when the above statement is used to initialise the ip_address; program execution subsequently fails here
    Code:
    strncat(subnet, &ch, 1);
    So the issue I have, is that the following code works fine:
    Code:
    fprintf(logfile , "   |-Source IP        : %s\n" , inet_ntoa(source.sin_addr) );
    Code:
    inet_ntoa(source.sin_addr)
    is treated as a string

    But when I try and use
    Code:
    strcpy(ip_address,inet_ntoa(source.sin_addr));
    As input, while the above statement executes fine. This statement fails
    Code:
    strncat(subnet, &ch, 1);
    Appreciate suggestions on how to workaround

    Thanks

    VW

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Calling strncat in a loop like that is rather inefficient since strncat has to find the end of the string and then append to it, plus using strncat with a lone char like that looks rather unconventional. I would suggest something like:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        // Initialise string
        char ip_address[16];
        char subnet[12];
        strcpy(ip_address,"192.168.200.200");
        strcpy(subnet,"");
    
        // Process string
        int i;
        int period_count = 0;
        int length = strlen(ip_address);
        for (i = 0; i < length; i++)
        {
            if (ip_address[i] == '.')
            {
                period_count++;
    
                if (period_count == 3)
                {
                    break;
                }
            }
    
            subnet[i] = ip_address[i];
        }
        subnet[i] = '\0';
    
        printf("Subnet: %s\n", subnet);
    
        return 0;
    }
    whether this also solves the crash that you're facing is another matter, but maybe it will.

    Note that this code assumes a valid IPv4 address. If the address comes from user input and is not validated, a buffer overflow vulnerability may be present.
    Last edited by laserlight; 03-17-2021 at 09:18 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2020
    Posts
    47

    Thumbs up

    Hello laserlight,

    Fixed the crash as well

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Process exited error c compiler
    By hamza el hattab in forum C Programming
    Replies: 3
    Last Post: 02-01-2020, 05:40 PM
  2. Compile error? Logic error? Syntax Error? Please help
    By Khody Afkhami in forum C Programming
    Replies: 4
    Last Post: 10-11-2014, 01:36 AM
  3. How to know last process exited dll
    By alexia in forum Windows Programming
    Replies: 5
    Last Post: 03-17-2010, 12:12 PM
  4. problem in gdbserver: killing inferior
    By ashok449 in forum C Programming
    Replies: 1
    Last Post: 03-30-2009, 05:57 AM
  5. "exited due to signal 10 (sigbus)"
    By pktcperlc++java in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2005, 10:07 AM

Tags for this Thread