Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ scope problem with --tracking in uncrustify.cpp #4216

Open
JEAYNE opened this issue Jan 31, 2024 · 0 comments
Open

C++ scope problem with --tracking in uncrustify.cpp #4216

JEAYNE opened this issue Jan 31, 2024 · 0 comments
Labels
Engineering Anything to do with building/releasing/deploying uncrustify; with CI; or with the test framework.

Comments

@JEAYNE
Copy link
Contributor

JEAYNE commented Jan 31, 2024

Demo (with WIndows)

> uncrustify.exe --version
Uncrustify-0.78.1_f

> echo float f1(int); | uncrustify.exe -c - -l cpp
main(1028): Parsing: 17 bytes (17 chars) from stdin as language CPP
float f1(int);

> echo float f1(int); | uncrustify.exe -c - -l cpp --tracking space:Space.html
main(1028): Parsing: 17 bytes (17 chars) from stdin as language CPP
uncrustify_file: Unable to create f1(int);
&Ý^ü☺: Invalid argument (22)

The root cause of this strange output Unable to create f1(int); and &Ý^ü☺: Invalid argument is a problem of scope in uncrustify.cpp

681: if (html_arg != nullptr)
682: {
          . . .
692:      char buffer[max_args_length];
693:      strcpy(buffer, html_arg);
694:
695:      // Tokenize and extract key and value
696:      const char *tracking_art = strtok(buffer, ":");
697:      const char *html_file    = strtok(nullptr, ":");
698:
699:      if (html_file != nullptr)
700:      {
             . . .
720:         cpd.html_file = html_file;  // <-- problem here
721:      }
722: } 

html_file points some where in buffer. But the scope of buffer is the block if (html_arg != nullptr){ ... }
and the global variable cpd.html_file requires a global scope.

With this fix:

699: if (html_arg != nullptr && cpd.html_file == nullptr)

720:         cpd.html_file = strdup(html_file); 

the output looks good:

> echo float f1(int); | uncrustify.exe -c - -l cpp --tracking space:Spaces.html
main(1028): Parsing: 17 bytes (17 chars) from stdin as language CPP

> type Space.html
<html>
<head>
 ...

Note:s

  1. the memory allocated by strdup() is used until the end of the program. It is therefore not absolutely necessary to free() this memory.
  2. the change made on line 699 is not mandatory because it is impossible to parse two --tracking, the second will be interpreted as a file name. But it may be clever to explicitly avoid memory leaks by showing that we will never call strdup() twice.

Regards.

@JEAYNE JEAYNE changed the title C++ scope problem with --tracking C++ scope problem with --tracking in uncrustify.cpp Feb 1, 2024
JEAYNE added a commit to JEAYNE/uncrustify.dev that referenced this issue Feb 3, 2024
@gmaurel gmaurel added the Engineering Anything to do with building/releasing/deploying uncrustify; with CI; or with the test framework. label Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Engineering Anything to do with building/releasing/deploying uncrustify; with CI; or with the test framework.
Projects
None yet
Development

No branches or pull requests

2 participants