dawnbeen / c_formatter_42

C language formatter for 42 norminette
GNU General Public License v3.0
176 stars 17 forks source link

Infinite loop if function call in a comment #58

Closed huiman closed 1 year ago

huiman commented 1 year ago

$ c_formatter_42 < file.c $ python3 -m c_formatter_42 < file.c

not showing any output and freeze.

cacharle commented 1 year ago

what is the content of file.c?
what is the version of the package?
what is your version of Python?
what OS are you on?

huiman commented 1 year ago

I just found the cause of the problem.

this is my c:

#include "libft.h"

/*
The  bzero()  function  erases  the data in the n bytes of the memory starting at the location pointed to by s, by writing zeros (bytes containing '\0') to that area.

The explicit_bzero() function performs the same task as bzero().  It differs from bzero() in  that  it  guarantees that compiler optimizations will not remove the erase operation if the compiler deduces that the operation is "un‐necessary".
*/
void    bzero(void *s, size_t n)
{
                    unsigned char *ptr_s;     
}

I just found out that 42 does not allow the comment above function.

cacharle commented 1 year ago

Indeed, the comment is the issue, it's still a bug because it shouldn't hang like this.

if I remove the bzero() in the first line of the comment, it doesn't hang.

cacharle commented 1 year ago

@keyhr, I see it hangs in the line_breaker function, do you have any idea why this behavior occurs?

(I don't think it needs to be correctly formatted but would be nice if it didn't hang)

❯ c_formatter_42 <t.c
^CTraceback (most recent call last):
  File "/usr/local/bin/c_formatter_42", line 33, in <module>
    sys.exit(load_entry_point('c-formatter-42', 'console_scripts', 'c_formatter_42')())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/git/c_formatter_42/c_formatter_42/__main__.py", line 43, in main
    print(run_all(content), end="")
          ^^^^^^^^^^^^^^^^
  File "~/git/c_formatter_42/c_formatter_42/run.py", line 38, in run_all
    content = line_breaker(content)
              ^^^^^^^^^^^^^^^^^^^^^
  File "~/git/c_formatter_42/c_formatter_42/formatters/line_breaker.py", line 8, in line_breaker
    lines = [insert_break(line, column_limit) for line in lines]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/git/c_formatter_42/c_formatter_42/formatters/line_breaker.py", line 8, in <listcomp>
    lines = [insert_break(line, column_limit) for line in lines]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/git/c_formatter_42/c_formatter_42/formatters/line_breaker.py", line 22, in insert_break
    line_indent_level = indent_level(line)
                        ^^^^^^^^^^^^^^^^^^
  File "~/git/c_formatter_42/c_formatter_42/formatters/line_breaker.py", line 84, in indent_level
    if re.match(align_pattern, line):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/pythonXX/site-packages/re/__init__.py", line 166, in match
    return _compile(pattern, flags).match(string)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt
huiman commented 1 year ago

I try update the comment and this format is working: [delete second line and replace double spaces with single]

/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_bzero.c                                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: npattana <superhuiman@gmail.com>           +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2023/08/28 14:57:50 by hui               #+#    #+#             */
/*   Updated: 2023/08/28 18:12:26 by npattana         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"

/*
The function erases the data in the n bytes of the memory starting at the
location pointed to by s, by writing zeros (bytes containing '\0') to that area.
*/
void    bzero(void *s, size_t n)
{
    unsigned char   *ptr_s;
}
younesaassila commented 1 year ago

Working on a fix...

cacharle commented 1 year ago

@huiman Can you test with the branch from #64 and tell us if it's fixed?

huiman commented 1 year ago

I have tested it, and the infinite loop that causes freezing is gone. Thanks.