Hello i'm getting a segmentation fault in my is_validport() function and could somebody people point out what i'm doing wrong please?

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>

/* Function definitions. */
//char *is_uid(char *s);
char *is_destportseq(char *s);
char *is_destport(char *s);
char *is_validport(char *s);
char *trimwhitespacestartandend(char *str);

/* Main method - driver. */
int main() {
    char *string = "80,  443, 8080";

    /*
        char *string2 = "65536";
        if(atoi(string2) <= 65535){
            if(0 <= atoi(string2)){
            printf("%s %s\n", string2, "Is Valid");
            }
        }else{
            printf("%s %s\n", string2, "Is NOT Valid");
        }
     */


    if (is_validport(string)) {
        printf("Valid PORTS.\n");
        return EXIT_SUCCESS;
    } else {
        printf("Invalid PORTS.\n");
        return EXIT_FAILURE;
    }

    /* The entire program is checking if our input is a sentences. */
    if (is_destportseq(string)) {
        printf("Valid sentence.\n");
        return EXIT_SUCCESS; /* Zero means success. */
    } else {
        printf("Invalid sentence.\n");
        return EXIT_FAILURE;
    }
}

char *is_destportseq(char *s) {
    char *r1, *r2; /* Report variables. */

    /* We recursively evaluate the string, passing back the pointer after each
       call. */
    r1 = is_destport(s);
    if (r1) {
        if (*r1 == 0) {
            return r1;
            /* Either it's a sentences or it isn't. */
        } else {
            if (*r1 == ',') {
                r1++;
            } else {
                return NULL;
            }
            while (*r1 == ' ') {
                r1++;
            }
            r2 = is_destportseq(r1);
            if (r2) {
                return r2;
            } else {
                return NULL;
            }
        }
    }

    return NULL;
}

char *is_destport(char *s) {
    if (!isdigit((int) * s)) {
        return NULL;
    }

    /* Remember, for a root element, you return one past. */
    while (isdigit((int) * s)) {
        s++;
    }
    return s;
}
//segmentation fault
char *is_validport(char *s) {
    char *firstport;
    char *temps;
    char *temps2;

    *temps = *s;
    //printf("s %s \n", s);
    //printf("temps %s \n", temps);
    firstport = strtok(s, ",");

    if (*s == 0) {
        return s;
    } else {
        if (atoi(firstport) <= 65535) {
            if (0 <= atoi(firstport)) {
                //printf("s = %s \n", s);
                temps2 = strstr(s, " ");
                temps2 = trimwhitespacestartandend(temps2);
                is_validport(temps2);
            }
        } else {
            return NULL;
        }
    }
    return NULL;
}

char *trimwhitespacestartandend(char *str) {
    char *end;
    // Trim leading space
    while (isspace((int) * str)) str++;
    // Trim trailing space
    end = str + strlen(str) - 1;
    while (end > str && isspace((int) * end)) end--;
    // Write new null terminator
    *(end + 1) = 0;
    return str;
}

its trying to break up a string "80, 443, 8080" into "80" "443" "8080" and make sure each number >=0 & <= 65535.


Thanks in advance.

Some lint:

--- Module: main.c (C++)
main.c 3 error 537: (Warning -- Repeated include file 'D:\Programs\CodeBlocks\MinGW\lib\gcc\mingw32\3.4.5\install-tools\include\stddef.h')
main.c 16 error 1776: (Info -- Converting a string literal to char * is not const safe (initialization))
main.c 39 error 527: (Warning -- Unreachable code at token 'if')
main.c 96 error 530: (Warning -- Symbol 'temps' (line 93) not initialized)
main.c 93 error 830: (Info -- Location cited in prior message)
main.c 104 error 668: (Warning -- Possibly passing a null pointer to function 'atoi(const char *)', arg. no. 1 [Reference: file main.c: line 99])
main.c 99 error 831: (Info -- Reference cited in prior message)
main.c 109 error 534: (Warning -- Ignoring return value of function 'is_validport(char *)' (compare with line 91))
main.c 91 error 830: (Info -- Location cited in prior message)

During Specific Walk:
File main.c line 30: is_validport(!=0)
main.c 104 error 668: (Warning -- Possibly passing a null pointer to function 'atoi(const char *)', arg. no. 1 [Reference: file main.c: line 99])
main.c 99 error 831: (Info -- Reference cited in prior message)

During Specific Walk:
File main.c line 109: is_validport(?)
main.c 104 error 668: (Warning -- Possibly passing a null pointer to function 'atoi(const char *)', arg. no. 1 [Reference: file main.c: line 99])
main.c 99 error 831: (Info -- Reference cited in prior message)

During Specific Walk:
File main.c line 108: trimwhitespacestartandend(0?)
main.c 121 error 613: (Warning -- Possible use of null pointer 'str' in argument to operator 'unary *' [Reference: file main.c: lines 107, 108])
main.c 107 error 831: (Info -- Reference cited in prior message)
main.c 108 error 831: (Info -- Reference cited in prior message)

During Specific Walk:
File main.c line 108: trimwhitespacestartandend(0?)
main.c 121 error 613: (Warning -- Possible use of null pointer 'str' in argument to operator '++' [Reference: file main.c: lines 107, 108])
main.c 107 error 831: (Info -- Reference cited in prior message)
main.c 108 error 831: (Info -- Reference cited in prior message)

During Specific Walk:
File main.c line 108: trimwhitespacestartandend(0?)
main.c 123 error 802: (Info -- Conceivably passing a null pointer to function 'strlen(const char *)', arg. no. 1 [Reference: file main.c: lines 107, 108])
main.c 107 error 831: (Info -- Reference cited in prior message)
main.c 108 error 831: (Info -- Reference cited in prior message)

During Specific Walk:
File main.c line 108: trimwhitespacestartandend(0?)
main.c 123 error 794: (Info -- Conceivable use of null pointer 'str' in left argument to operator 'ptr+int' [Reference: file main.c: lines 107, 108])
main.c 107 error 831: (Info -- Reference cited in prior message)
main.c 108 error 831: (Info -- Reference cited in prior message)

[edit]You can't write here:

char *is_validport(char *s) {
    char *firstport;
    char *temps;
    char *temps2;

    *temps = *s;

because temps doesn't point anywhere.

[edit=2]D'oh!

But you can't use strtok on a string literal:

char *string = "80,  443, 8080";

[edit=3]Do you really need all that code? Are you doing something like this?

#include <stdio.h>

void foo(const char *string)
{
   int a, b, c;
   if ( sscanf(string, "%d, %d, %d", &a, &b, &c) == 3 )
   {
      if ( a >= 0 && a <= 65535 &&
           b >= 0 && b <= 65535 &&
           c >= 0 && c <= 65535 )
      {
         printf("a = %d, b = %d, c = %d\n", a, b, c);
      }
   }
}

int main() 
{
   foo("80,  443, 8080");
   return 0;
}

/* my output
a = 80, b = 443, c = 8080
*/

I'm sorry but I don't really understand that inorder to fix it.

Before i added

firstport = strtok(s, ",");

i had it in my main function as and i could print out the result as "80" from the original string but i don't know what i can't use it in that function.

also i've changed it so that atoi shouldn't process a null value:

char *is_validport(char *s) {
    char *firstport;
    char *temps;
    char *temps2;

    temps = s;
    //printf("s %s \n", s);
    //printf("temps %s \n", temps);
    if (*s == 0) {
        return s;
    } else {
        firstport = strtok(s, ",");
        if (*firstport == 0) {
            return s;
        } else {
            if (atoi(firstport) <= 65535) {
                if (0 <= atoi(firstport)) {
                    //printf("s = %s \n", s);
                    temps2 = strstr(s, " ");
                    temps2 = trimwhitespacestartandend(temps2);
                    is_validport(temps2);
                }
            } else {
                return NULL;
            }
        }
    }
    return NULL;
}

I would do it like [edit=3] however i don't know how many different numbers i will get so i was doing it recursively. As the string is valid as long as it matchs this BNF grammar

<dest port seq> ::= <destination port> | <destination port>','<spaces><dest port seq> | <destination port>','<dest port seq>

It will make sure the string is valid for the grammar. the problem is making sure each port >=0 & <= 65535 for a n number of ports

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.