Skip to content

Commit

Permalink
✨ feat(errorhandling): add error handling and few touch for 42 correc…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
Jibus22 committed Sep 7, 2022
1 parent b1adcf7 commit 0445838
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ else
endif

##### SRCS #####
SRCS = $(addprefix $(SRCPATH)/, start.c ft_nm_x64.c ft_nm_x32.c print_debug.c\
format_output.c sorting.c shared.c)
SRCS = $(addprefix $(SRCPATH)/, start.c ft_nm_x64.c ft_nm_x32.c \
format_output.c sorting.c shared.c error.c)

OBJ = $(SRCS:$(SRCPATH)/%.c=$(OBJPATH)/%.o)

Expand Down
3 changes: 2 additions & 1 deletion libft/ft_rbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ void destroy_rbt(t_rbt *node) {
void print_rbt_inorder(t_rbt *node) {
if (node) {
print_rbt_inorder(node->left);
printf("%s", (char *)node->value);
write(STDOUT_FILENO, (char *)node->value, ft_strlen((char *)node->value));
/* printf("%s", (char *)node->value); */
print_rbt_inorder(node->right);
}
}
Expand Down
30 changes: 30 additions & 0 deletions srcs/error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "ft_nm.h"

int error_wrap(unsigned int err, t_rbt *root, char *output_buf) {
if (root) destroy_rbt(root);
if (output_buf) free(output_buf);
return oops_error(err);
}

int oops_error(unsigned int err) {
static struct {
int err_nb;
char oops_msg[OOPS_SIZE];
} oops[OOPS_NB] = {
{OOPS_UND, "undefined err\n\0"},
{OOPS_NOTELF, "file isn't ELF\n\0"},
{OOPS_NOCAVE, "no cave found\n\0"},
{OOPS_BAD_ARG_NB, "wrong number of arguments\n\0"},
{OOPS_BAD_ELF, "elf corrupted\n\0"},
{OOPS_NO_LOAD, "no LOAD segment found\n\0"},
{OOPS_BAD_PHDR, "program header corrupted\n\0"},
{OOPS_BAD_SHDR, "section header corrupted\n\0"},
{OOPS_BAD_SYMTAB, "symtab unreachable or corrupted\n\0"},
{OOPS_BAD_STRTAB, "strtab unreachable or corrupted\n\0"},
};

if (err > OOPS_NB) err = OOPS_UND;
write(STDERR_FILENO, "nm error: ", 10);
write(STDERR_FILENO, oops[err].oops_msg, ft_strlen(oops[err].oops_msg));
return EXIT_FAILURE;
}
29 changes: 19 additions & 10 deletions srcs/ft_nm_x32.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ static int buffer_nm(const Elf32_Ehdr *Ehdr, const Elf32_Shdr *Shdrt,
for (int i = 1; i < symb_nb; i++) {
if (Ssymtab[i].st_shndx > Ehdr->e_shnum &&
!is_special_section_indice(Ssymtab[i].st_shndx))
return EXIT_FAILURE;
return error_wrap(OOPS_BAD_SYMTAB, root, output_buf);
if (is_special_section_indice(Ssymtab[i].st_shndx) ||
ELF32_ST_TYPE(Ssymtab[i].st_info) == STT_SECTION)
continue;
shstrtabndx = Shdrt[Ssymtab[i].st_shndx].sh_name;
if (shstrtabndx > smax[SHSTRTAB] || Ssymtab[i].st_name > smax[STRTAB])
return EXIT_FAILURE;
return error_wrap(OOPS_BAD_STRTAB, root, output_buf);
format_output(&root, Ssymtab[i].st_value, &Sstrtab[Ssymtab[i].st_name],
Ssymtab[i].st_info, Ssymtab[i].st_shndx,
&shstrtab[shstrtabndx], 32);
Expand Down Expand Up @@ -52,7 +52,7 @@ static int handle_symtab(const void *file, const Elf32_Ehdr *Ehdr,
filesize < Shdrt[symtab].sh_offset + Shdrt[symtab].sh_size ||
filesize <
Shdrt[Ehdr->e_shstrndx].sh_offset + Shdrt[Ehdr->e_shstrndx].sh_size)
return EXIT_FAILURE;
return oops_error(OOPS_BAD_SYMTAB);

Sstrtab = (char *)(file + Shdrt[strtab].sh_offset);
Sshstrtab = (char *)(file + Shdrt[Ehdr->e_shstrndx].sh_offset);
Expand All @@ -72,25 +72,34 @@ static int handle_symtab(const void *file, const Elf32_Ehdr *Ehdr,
int ft_nm_x32(const void *file, size_t filesize) {
Elf32_Ehdr Ehdr;
Elf32_Shdr *Shdrt;
int symtab = -1;
int symtab = -1, ret;

if (filesize < sizeof(Ehdr)) return EXIT_FAILURE;
if (filesize < sizeof(Ehdr)) return oops_error(OOPS_NOTELF);
ft_memcpy(&Ehdr, file, sizeof(Ehdr));
if (!Ehdr.e_shnum || !Ehdr.e_shoff || Ehdr.e_version == EV_NONE ||
filesize < Ehdr.e_shoff + (sizeof(*Shdrt) * Ehdr.e_shnum) ||
Ehdr.e_shentsize != sizeof(*Shdrt))
return EXIT_FAILURE;
return oops_error(OOPS_BAD_ELF);

/* print_Ehdr(&Ehdr); */
Shdrt = (Elf32_Shdr *)(file + Ehdr.e_shoff);

for (int i = 0; i < Ehdr.e_shnum && symtab == -1; i++) {
if (Shdrt[0].sh_size != 0 && Shdrt[0].sh_offset != 0)
return oops_error(OOPS_BAD_SHDR);
if (Ehdr.e_shstrndx >= Ehdr.e_shnum ||
Shdrt[Ehdr.e_shstrndx].sh_type != SHT_STRTAB) {
oops_error(OOPS_BAD_SYMTAB);
return EXIT_SUCCESS;
}
for (int i = 0; i < Ehdr.e_shnum; i++) {
if (Shdrt[i].sh_name > Shdrt[Ehdr.e_shstrndx].sh_size)
return oops_error(OOPS_BAD_SHDR);
if (Shdrt[i].sh_type == SHT_SYMTAB) symtab = i;
/* print_Shdr(&(Shdrt[i]), i); */
}
if (symtab > -1)
handle_symtab(file, &Ehdr, Shdrt, symtab, filesize);
ret = handle_symtab(file, &Ehdr, Shdrt, symtab, filesize);
else
return EXIT_FAILURE;
return EXIT_SUCCESS;
return oops_error(OOPS_BAD_SYMTAB);
return ret;
}
28 changes: 16 additions & 12 deletions srcs/ft_nm_x64.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ static int buffer_nm(const Elf64_Ehdr *Ehdr, const Elf64_Shdr *Shdrt,
for (int i = 1; i < symb_nb; i++) {
if (Ssymtab[i].st_shndx > Ehdr->e_shnum &&
!is_special_section_indice(Ssymtab[i].st_shndx))
return EXIT_FAILURE;
return error_wrap(OOPS_BAD_SYMTAB, root, output_buf);
if (is_special_section_indice(Ssymtab[i].st_shndx) ||
ELF64_ST_TYPE(Ssymtab[i].st_info) == STT_SECTION)
continue;
shstrtabndx = Shdrt[Ssymtab[i].st_shndx].sh_name;
if (shstrtabndx > smax[SHSTRTAB] || Ssymtab[i].st_name > smax[STRTAB])
return EXIT_FAILURE;
return error_wrap(OOPS_BAD_STRTAB, root, output_buf);
format_output(&root, Ssymtab[i].st_value, &Sstrtab[Ssymtab[i].st_name],
Ssymtab[i].st_info, Ssymtab[i].st_shndx,
&shstrtab[shstrtabndx], 64);
Expand Down Expand Up @@ -52,7 +52,7 @@ static int handle_symtab(const void *file, const Elf64_Ehdr *Ehdr,
filesize < Shdrt[symtab].sh_offset + Shdrt[symtab].sh_size ||
filesize <
Shdrt[Ehdr->e_shstrndx].sh_offset + Shdrt[Ehdr->e_shstrndx].sh_size)
return EXIT_FAILURE;
return oops_error(OOPS_BAD_SYMTAB);

Sstrtab = (char *)(file + Shdrt[strtab].sh_offset);
Sshstrtab = (char *)(file + Shdrt[Ehdr->e_shstrndx].sh_offset);
Expand All @@ -72,30 +72,34 @@ static int handle_symtab(const void *file, const Elf64_Ehdr *Ehdr,
int ft_nm_x64(const void *file, size_t filesize) {
Elf64_Ehdr Ehdr;
Elf64_Shdr *Shdrt;
int symtab = -1;
int symtab = -1, ret;

if (filesize < sizeof(Ehdr)) return EXIT_FAILURE;
if (filesize < sizeof(Ehdr)) return oops_error(OOPS_NOTELF);
ft_memcpy(&Ehdr, file, sizeof(Ehdr));
if (!Ehdr.e_shnum || !Ehdr.e_shoff || Ehdr.e_version == EV_NONE ||
filesize < Ehdr.e_shoff + (sizeof(*Shdrt) * Ehdr.e_shnum) ||
Ehdr.e_shentsize != sizeof(*Shdrt))
return EXIT_FAILURE;
return oops_error(OOPS_BAD_ELF);

/* print_Ehdr(&Ehdr); */
Shdrt = (Elf64_Shdr *)(file + Ehdr.e_shoff);

if (Shdrt[0].sh_size != 0 && Shdrt[0].sh_offset != 0) return EXIT_FAILURE;
if (Shdrt[0].sh_size != 0 && Shdrt[0].sh_offset != 0)
return oops_error(OOPS_BAD_SHDR);
if (Ehdr.e_shstrndx >= Ehdr.e_shnum ||
Shdrt[Ehdr.e_shstrndx].sh_type != SHT_STRTAB)
Shdrt[Ehdr.e_shstrndx].sh_type != SHT_STRTAB) {
oops_error(OOPS_BAD_SYMTAB);
return EXIT_SUCCESS;
}
for (int i = 0; i < Ehdr.e_shnum; i++) {
if (Shdrt[i].sh_name > Shdrt[Ehdr.e_shstrndx].sh_size) return EXIT_FAILURE;
if (Shdrt[i].sh_name > Shdrt[Ehdr.e_shstrndx].sh_size)
return oops_error(OOPS_BAD_SHDR);
if (Shdrt[i].sh_type == SHT_SYMTAB) symtab = i;
/* print_Shdr(&(Shdrt[i]), i); */
}
if (symtab > -1)
handle_symtab(file, &Ehdr, Shdrt, symtab, filesize);
ret = handle_symtab(file, &Ehdr, Shdrt, symtab, filesize);
else
return EXIT_FAILURE;
return EXIT_SUCCESS;
return oops_error(OOPS_BAD_SYMTAB);
return ret;
}
20 changes: 20 additions & 0 deletions srcs/includes/ft_nm.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@
#define STRTAB 0
#define SHSTRTAB 1

#define OOPS_SIZE 48

typedef enum {
OOPS_UND = 0,
OOPS_NOTELF,
OOPS_NOCAVE,
OOPS_BAD_ARG_NB,
OOPS_BAD_ELF,
OOPS_NO_LOAD,
OOPS_BAD_PHDR,
OOPS_BAD_SHDR,
OOPS_BAD_SYMTAB,
OOPS_BAD_STRTAB,
OOPS_NB,
} e_oops;

/* tuple to link section name with nm symbol type */
typedef struct {
char nmType;
Expand Down Expand Up @@ -63,4 +79,8 @@ int is_special_section_indice(uint16_t section_index);
unsigned int rbt_to_buf(t_rbt *node, char *output_buf, unsigned int ret);
bool getargs(e_arg env);

/* error.c */
int oops_error(unsigned int err);
int error_wrap(unsigned int err, t_rbt *root, char *output_buf);

#endif
7 changes: 4 additions & 3 deletions srcs/start.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ static int ft_nm(int ac, char *av, int j) {
if ((fd = open(av, O_RDONLY)) == -1)
return exit_nm(EXIT_FAILURE, fd, NULL, 0, av, "No such file\n");
if (fstat(fd, &statbuf) == -1)
return exit_nm(EXIT_FAILURE, fd, NULL, 0, NULL, NULL);
return exit_nm(EXIT_FAILURE, fd, NULL, 0, NULL, "fstat() failed\n");
if (statbuf.st_size < 16)
return exit_nm(EXIT_FAILURE, fd, NULL, 0, NULL, NULL);
return exit_nm(EXIT_FAILURE, fd, NULL, 0, NULL,
"file format not recognized\n");
if ((file = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) ==
MAP_FAILED)
return exit_nm(EXIT_FAILURE, fd, NULL, 0, NULL, NULL);
return exit_nm(EXIT_FAILURE, fd, NULL, 0, NULL, "mmap() failed\n");
if (check_elf_ident(file, &arch) > 0)
return exit_nm(EXIT_FAILURE, fd, file, statbuf.st_size, av,
"file format not recognized\n");
Expand Down
3 changes: 3 additions & 0 deletions test/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ print_help () {

run_comparison () {
local arg="$1"
local err_nb=0

if [ "$1" == "-a" -o "$1" == "-g" -o "$1" == "-u" -o \
"$1" == "-r" -o "$1" == "-p" ]; then
Expand Down Expand Up @@ -93,8 +94,10 @@ run_comparison () {
rm $mylog $ft_res $res;
else
echo -e "${red}KO! ❌ ${blue} check this-> ${white}$mylog${reset}";
((err_nb=err_nb+1));
fi
done
echo "$err_nb errors."
}

rm -f $LOG*.txt
Expand Down

0 comments on commit 0445838

Please sign in to comment.