I'm developing a mini-sql in c and I'm using the fgets function, but when I run the program a file called "raise.c" is created (with a message of "Couldn't load source './signal/../ sysdeps/unix/sysv/linux/raise.c': 'SourceRequest' not supported.." and the terminal says "malloc(): corrupted top size" I've already put malloc in the used strings and tested several values, but the error persists. I ran the vscode debugger and found that the error is on line 180 of the code (the one in bold), it's some problem with memory allocation, but I'm new to c so I can't solve it, here's my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct{
char ***Colunas;
int indice;
} resposta;
typedef struct{
char **nome; // lista com o nome dos arquivos
int numeroArq; // quantidade
} locaisArq;
typedef struct {
char **variaveisWhere;
char **comandosWhere;
char **valoresEsperadoswhere;
int verifica;
int **indices;
} comparacaoWhere;
int verificaWhere (comparacaoWhere *where){
if (where->verifica){
return 1;
}
else {return 0;}
}
void lerValoresColuna (int indice, resposta **valoresColuna, resposta *valorLinha, comparacaoWhere *where){
if(verificaWhere(where)){
//Vai separa valosres mostrar de acordo com where
}
else{
//Mostra todos valores coluna
}
}
int listaPalavras(char *entrada, char **lista, char *separador){
char *splitStr;
int qtnPalavras = 0;
splitStr = strtok(entrada, separador);
while (splitStr != NULL){
int qtdCaracter;
qtdCaracter = sizeof(splitStr);
lista[qtnPalavras] = malloc(qtdCaracter * sizeof(char));
if(lista[qtnPalavras] == NULL){return 0;}
strcpy(lista [qtnPalavras], splitStr);
splitStr = strtok(NULL, separador);
qtnPalavras++;
}
free(entrada);
return qtnPalavras;
}
//Desaloca listas de matriz
void desalocarLista(char **lista, int qtdLinha){
for(int i = 0; i < qtdLinha; i++){free(lista[i]);}
free (lista);
}
void desalocarTudo( locaisArq *selec, locaisArq *nomeArq, comparacaoWhere *where){
desalocarLista(selec->nome, selec->numeroArq);
free(selec);
desalocarLista(nomeArq->nome, nomeArq->numeroArq);
free(nomeArq);
int qtdValores;
qtdValores = sizeof(where->indices);
desalocarLista(where->variaveisWhere,qtdValores);
desalocarLista(where->comandosWhere,qtdValores);
desalocarLista(where->valoresEsperadoswhere,qtdValores);
free(where);
};
int atribuiInfo(char **lista, int palavras, locaisArq *selec, locaisArq *nomeArq, comparacaoWhere *where){
int i = 1; // começa na segunda palavra, pois a primeira é sempre Select
selec->numeroArq = 0;
nomeArq->numeroArq = 0;
int qtdCaracter;
do{
qtdCaracter = sizeof(lista[i]);
selec->nome[(selec->numeroArq)] = malloc(qtdCaracter * sizeof(char));
if(selec->nome[(selec->numeroArq)] == NULL){return 0;}
strcpy (selec->nome[(selec->numeroArq)], lista[i]);
i++;
(selec->numeroArq)++;
if (i == (palavras-1)){break;}
} while ((strcmp (lista[i], "from")) != 0);
i++; // pula o from
do {
qtdCaracter = sizeof(lista[i]);
nomeArq->nome[(nomeArq->numeroArq)] = malloc(qtdCaracter * sizeof(char));
if(nomeArq->nome[(nomeArq->numeroArq)] == NULL){return 0;}
strcpy (nomeArq->nome[(nomeArq->numeroArq)], lista[i]);
i++;
(nomeArq->numeroArq)++;
if (i == (palavras-1)){break;}
} while (i<palavras && (strcmp (lista[i], "where")) != 0);
i++; // pula a palavra "Where"
while (i < palavras){
int j = i; //o "0" a partir do where
if ((i-j) % 4 == 0){
qtdCaracter = sizeof(lista[i]);
where->variaveisWhere[(i-j)] = malloc(qtdCaracter * sizeof(char));
if(where->variaveisWhere[(i-j)] == NULL){return 0;}
strcpy (where->variaveisWhere[(i-j)], lista[i]);
}
else if ((i-j) % 4 == 1){
qtdCaracter = sizeof(lista[i]);
where->comandosWhere[(i-j)] = malloc(qtdCaracter * sizeof(char));
if( where->comandosWhere[(i-j)] == NULL){return 0;}
strcpy (where->comandosWhere[(i-j)], lista[i]);
}
else if ((i-j) % 4 == 2) {
qtdCaracter = sizeof(lista[i]);
where->valoresEsperadoswhere[(i-j)] = malloc(qtdCaracter * sizeof(char));
if(where->valoresEsperadoswhere[(i-j)] == NULL){return 0;}
strcpy (where->valoresEsperadoswhere[(i-j)], lista[i]);
where->verifica = 1;
}
i++;
}
desalocarLista(lista, palavras);
return 1;
}
int main(int argc, char *argv[]) {
char *entrada;
entrada = malloc(2056 * sizeof(char));
if(entrada == NULL){return 0;}
//Perguntar se realmente vamos usar no desenvolvimento, pq ja temos struck, caso não, não preisa alocar
char **listaEntrada;
listaEntrada = malloc (300* sizeof(char));
if(listaEntrada == NULL){return 0;}
fgets(entrada, 2056, stdin);
int palavras;
palavras = listaPalavras(entrada, listaEntrada, " ,\n");
locaisArq *selec;
selec = malloc(1 * sizeof(locaisArq));
if(selec == NULL){return 0;}
locaisArq *nomeArq;
nomeArq = malloc(1 * sizeof(locaisArq));
if(nomeArq == NULL){return 0;}
comparacaoWhere *where;
where = malloc(1 * sizeof(comparacaoWhere));
if(where == NULL){return 0;}
where->verifica = 0;
atribuiInfo (listaEntrada, palavras, selec, nomeArq, where);
FILE *arquivo;
resposta resultado;
int qtdComandoWhere;
qtdComandoWhere = sizeof(where->indices);
for (int i = 0; i < nomeArq->numeroArq; i++){ // for que lê todos os arquivos listados
strcat(nomeArq->nome[i], ".txt");
arquivo = fopen(nomeArq->nome[i], "r"); // abre o arquivo especifico
if (arquivo == NULL){
printf("Erro ao abrir o arquivo");
}
char *stringLinha;
stringLinha = malloc(1000 * sizeof(char));// Ver quantas linhas colocar diretor
if(stringLinha == NULL){return 0;}
fflush(stdin);
fgets(stringLinha, sizeof(stringLinha), arquivo); // lê a primeira linha do arquivo
char **valoresLinha;
valoresLinha = malloc (1 * sizeof(char));
if(valoresLinha == NULL){return 0;}
resultado.indice = listaPalavras (stringLinha, valoresLinha, "\t");
int j = 0; // qual indice
while (j < resultado.indice){
if (strcmp(valoresLinha [resultado.indice], selec->nome[i])){ // confere se o indice é igual ao nome
int posiLinha = 0;
while (fgets(stringLinha, 2056, arquivo)){
listaPalavras(stringLinha, valoresLinha, "\t");
strcpy((resultado.Colunas[i][posiLinha]), (valoresLinha[j]));
printf("%s", resultado.Colunas[i][posiLinha]);
}
}
j++;
}
if (verificaWhere(where)){
while (j < resultado.indice){
int idx = 0;
while (idx < qtdComandoWhere){
if (strcmp(valoresLinha [resultado.indice], where->variaveisWhere[idx])){ // confere se o indice é igual ao nome
int posiLinha = 0;
int indice = 0;
while (fgets(stringLinha, 2056, arquivo)){
listaPalavras(stringLinha, valoresLinha, "\t");
strcpy (where->valoresEsperadoswhere[idx], (strtok(where->valoresEsperadoswhere[idx], "\"")));
if (valoresLinha[j] == where->valoresEsperadoswhere[idx]){
where->indices [i][indice] = j;
indice++;
}
}
idx++;
}
j++;
}
}
int qtdLinhaValoresLinha;
qtdLinhaValoresLinha = sizeof(valoresLinha);
desalocarLista(valoresLinha, qtdLinhaValoresLinha);
fclose(arquivo);
printf("foi") ;
}
}
desalocarTudo(selec, nomeArq, where);
}