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

标签 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

我的 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;
}

我的堆栈.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;

我的堆栈.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>

谢谢!!!!

最佳答案

在你的函数中readFile() , 这里:

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

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

关于gcc - 我的代码适用于 gcc 4.6,但不适用于 gcc 4.7(或更高版本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19849973/

相关文章:

c - 错误 : storage size of 'name' isn't known

c - C中_start()有什么用?

c - 对 `sqlcxt' 的 undefined reference

java - 尝试运行 Tarjan 算法的 java 实现

java - 从Java中的文件读取错误

c++ - 尝试将对象插入空指针数组时出现段错误

c - 小C程序中的段错误

c - 尝试释放()链表节点会导致 C 中的段错误

C程序编译错误(带头文件)

java.lang.NoClassDefFoundError 如何解决这个问题?