我的代码适用于gcc 4.6,但不适用于gcc 4.7(或更新版本)

时间:2013-11-08 01:24:34

标签: gcc compiler-errors segmentation-fault stack

如果我在gcc 4.6中编译我的代码,它可以工作。但是如果我在gcc 4.7(或更新版本)上编译,我会得到分段错误。

顺便说一下,我的代码应该分析一个html文件并说明它是否正确,都是使用堆栈完成的。

我正在使用gcc -Wall * .c -o EXE进行编译 使用./EXE site.html运行

gdb说:

编程接收信号SIGSEGV,分段故障。 来自/lib/x86_64-linux-gnu/libc.so.6的getc()中的0x00007ffff7abeb9a

my main.c

#include <stdio.h>
#include <string.h>
#include "stack.h"

int  readFile(char * nome, int m, int n, char out[m][n])
{
    char temp1[1];
    char temp2[50];
    int i= 0;
    char in;
    FILE * site = fopen(nome, "r");
    if(site == NULL) printf("\nArquivo não abriu");
    //in = (char)fgetc (site);
    //printf("%c", in);

    do
    {
        in = (char)fgetc (site);
        //printf("%c", in);
        //if(in == EOF) break;
        if(in == '<')
        {
            strcpy(temp2, "");
            while(1)
            {
                in = (char)fgetc (site);
                if(in == EOF || in == '>' || in == ' ') break;
                sprintf(temp1, "%c", in);
                printf("%c", in);
                strcat(temp2, temp1);
            }
            if(
                strcmp(temp2, "!DOCTYPE") == 0
                ||strcmp(temp2, "p") == 0
                || strcmp(temp2, "br") == 0
                || strcmp(temp2, "input") == 0
                || strcmp(temp2, "img") == 0
                || strcmp(temp2, "frame") == 0
                || strcmp(temp2, "li") == 0
            )
            {

            }
            else
            {
                strcpy(out[i], temp2);
                printf("\nEncontrou %s", temp2);
                i++;
            }
        }
    }
    while(in != EOF);
            fclose (site);
    return i;
}


int main(int argc, char * argv[])
{
    if (argc != 2)
    {
        printf("\nColoque algum arquivo HTML na entrada do programa\n\n");
        return(-1);
    }
    stack_p verificador = NULL;

    char out[1000][1000];
    char aux[50];
    int i = 0;
    int quant;

    if(create(&verificador, 50 * sizeof(char)) == SUCESS)
    {
        quant = readFile(argv[1], 1000, 1000, out);

    }
    else return -1;

    printf("\n\n");

    for(i = 0; i< quant; i++)
    {

        if(out[i][0] != '/')
        {
            push(verificador,(void *) &out[i]);
            printf("\n Empilhou %s", out[i]);
        }
        else if( out[i][0] == '/' )
        {
            searchTop(verificador,(void *) &aux);
            //printf("\n no topo esta %s", aux);
            if( strcmp(aux, &out[i][1]) == 0 )
            {
                pop(verificador);
                printf("\n desempilhou %s", aux);
            }
            else
            {
                printf("\n ERRO: ");
                printf("Esperava /%s, mas recebeu %s \n", aux, out[i] );
                return -1;
            }
        }

        printf("\n\n");

    }

    printf("\nO arquivo está correto!!\n\n");

    return 0;
}

我的stack.h

#include <stdlib.h>
#include <string.h>

#define FREE 0
#define BUSY 1
#define FAILED 0
#define SUCESS 1
#define TRUE 1
#define FALSE 0
#define EMPTY -1
#define YES 1
#define NO 0


typedef struct stack *stack_p, **stack_pp;

int create(stack_pp crtStack, int packSize);
void kill(stack_pp crtStack);
int searchTop(stack_p crtStack, void *reader);

int push(stack_p crtStack, void *new);
int pop(stack_p crtStack);

int length_stack(stack_p crtStack);

我的privateDataStack.h

#include "stack.h"

typedef struct node
{
    void *data; 
    struct node *next;
}node;

typedef struct stack
{
    int packSize;
    node* top;
}stack;

我的stack.c

#include "privateDataStack.h"
#include <stdio.h>
#include <unistd.h>

/*--------------------------------------------------------------------*/


int create(stack_pp crtStack, int packSize)
{
    if(( (*crtStack) =(stack_p) malloc(sizeof(stack)) ) == NULL)
    {
        return FAILED;
    }
    else
    {
        (*crtStack)->packSize = packSize;
        (*crtStack)->top = NULL;
        return SUCESS;
    }
}

/*--------------------------------------------------------------------*/


void kill(stack_pp crtStack)
{
    (**crtStack).top = NULL;
}

/*--------------------------------------------------------------------*/


int push(stack_p crtStack, void *new)
{
    node *myNode;

    if((myNode = (node*) malloc(sizeof(node)) ) == NULL)
    {
        return FAILED;
    }
    else
    {
        if((myNode->data = (void*) malloc(crtStack->packSize))
        == NULL)
        { 
            free(myNode);
            return FAILED;
        }
        else
        {
            memcpy(myNode->data, new, crtStack->packSize);
            myNode->next = crtStack->top;
            crtStack->top = myNode;
            return SUCESS;

        }
    }
}

/*--------------------------------------------------------------------*/

int pop(stack_p crtStack)
{
    if( (*crtStack).top == NULL )
    {
        return FAILED;
    }
    else
    {
        crtStack->top = crtStack->top->next;
        return SUCESS;
    }
}

/*--------------------------------------------------------------------*/

int searchTop(stack_p crtStack, void *reader)
{
    if(crtStack->top != NULL)
    {
        memcpy(reader, crtStack->top->data, 
        crtStack->packSize);
        return SUCESS;
    }
    else
    {
        return FAILED;
    }
}

int length_stack(stack_p crtStack)
{
    int cont = 0;
    node *aux;

    if(crtStack->top != NULL)
    { 
        cont++;
        aux = crtStack->top;
        while(aux->next != NULL)
        {
            aux = (node*)(aux->next);
            cont ++;
        }
    }

    return cont;
}

一些html来测试

<!DOCTYPE html>
<html>
    <head>
        <title>Title of the document</title>
    </head>

    <body>
        The content of the document......
    </body>
</html>

谢谢!!!!

1 个答案:

答案 0 :(得分:0)

在您的函数readFile()中,在此处:

    char temp1[1];
    /* ... */
    sprintf(temp1, "%c", in);

...正在触发缓冲区溢出。