minishell/src/treedrawing.c
Khaïs COLIN 76b2b8ec7f
treedrawing: fix bug where rare circumstances could lead to incorrect indent
The problem took me a while to find, but the issue here was that push_buf does
not add a terminating null byte, and I was only asking push_buf to copy the
bytes that contained characters, not the terminating null byte as well.

This, combined with the fact that to remove bytes from the end of the buffer, I
just placed a null byte a the end, caused some combinations of indentation and
dedentations to overwrite the null byte, and not place a null byte after the new
indent, which would still contain data from a previous indentation.
2025-03-28 14:50:37 +01:00

46 lines
1.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* treedrawing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/18 15:24:50 by khais #+# #+# */
/* Updated: 2025/03/19 14:00:35 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "treedrawing.h"
#include "libft.h"
void indent(t_buffer *leader, bool is_last)
{
ft_printf("%s", leader->buffer);
if (is_last)
{
ft_printf("%s", CORNER);
ft_buffer_push_buf(leader, SPACE, ft_strlen(SPACE));
}
else
{
ft_printf("%s", CROSS);
ft_buffer_push_buf(leader, VERTICAL, ft_strlen(VERTICAL));
}
}
void dedent(t_buffer *leader, bool is_last)
{
size_t length_to_remove;
ssize_t idx;
if (is_last)
length_to_remove = ft_strlen(SPACE);
else
length_to_remove = ft_strlen(VERTICAL);
idx = leader->length - length_to_remove;
if (idx >= 0)
{
ft_bzero(leader->buffer + idx, length_to_remove);
leader->length = idx;
}
}