[SLUG] evil C question! AAAGGHH!! help !!

From: Glen (gurensan@tampabay.rr.com)
Date: Tue Jun 19 2001 - 21:11:43 EDT


Dear list,

        I have been having trouble with something very odd. Here is my global struct:

struct{
        int BAUD;
        int PARITY;
        int DATA;
        int STOP;
        int FLOW;
        int FLAGS;
        char *PORT;
}comm_settings;

When I call a certain function, the char * variable in the struct gets
killed. Observe:

<snip>

fprintf(stdout,"PORT = %s\n", comm_settings.PORT);
fprintf(stdout,"FLOW = %d\n", comm_settings.FLOW);

send_error = send_data(buffer);

<snip>

comm_settings is a global struct with PORT being a char *. The call to
fprintf() above displays the correct value. In the call to send_data(), we
have this:

<snip>

if((port_fd = open(comm_settings.PORT, comm_settings.FLAGS)) == -1){
        fprintf(stderr, "error: port %s could not be opened: ", comm_settings.PORT);
        perror("");
        exit(1);
}

<snip>
At the beginning ot the send_data() function, the value of comm_settings.PORT
is "/dev/ttyS1". After entry into send_data(), it gets changed to
"*\021@v/ttyS1". This is weird, but gets better. After the call to open()
above, it changes to "?\025@Luy?" (I can't type some of these chars, but this
is close). Sometime before/during/after the call to fprintf() it, it changes
to an empty string and fprintf() prints nothing. The rest of the code not
shown is very straightforward.

        I have _never_ seen or heard of this kind of 'struct smashing'
before. Anyone got any ideas?

        Glen

        P.S. Yes, this will most likely be POSIX termios compliant and LGPL'd, if
anyone was wondering.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 19:22:45 EDT