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

src/mjson.c: unused variable suspected by clang #51

Open
git001 opened this issue Sep 18, 2022 · 2 comments
Open

src/mjson.c: unused variable suspected by clang #51

git001 opened this issue Sep 18, 2022 · 2 comments

Comments

@git001
Copy link
Contributor

git001 commented Sep 18, 2022

Clang creates a warning about unused variables which was discovered by HAPoxy community haproxy/haproxy#1868

One suggestion is to create a pragma for clang haproxy/haproxy#1868 (comment)
Another option is to add a new option like -D MJSON_DEBUG=1 and change the code accordingly.

Something like the code below instead of this block https://github.com/cesanta/mjson/blob/master/src/mjson.c#L186-L192.

static int plen1(const char *s) {
  int i = 0;
#if MJSON_DEBUG
  int n = 0;
#endif

  while (s[i] != '\0' && s[i] != '.' && s[i] != '[') {
    i += s[i] == '\\' ? 2 : 1;
#if MJSON_DEBUG
    n++;
#endif
  }

#if MJSON_DEBUG
  printf("PLEN1: s: [%s], [%.*s] => %d\n", s, i, s, n);
#endif

  return n;
}

As I would like to create a Pull Request to mjson and then add the new version to HAProxy please let me know what's your preferred way is to solve the clang warning.

@chipitsine
Copy link

this has raised just few days ago because is was detected by Fedora Rawhide which has switched to clang-15.
I guess it is new rule in clang-15

@jcorporation
Copy link

jcorporation commented Oct 22, 2022

I stumbled also about this issue:

/myMPD/dist/mjson/mjson.c:195:14: error: variable 'n' set but not used [-Werror,-Wunused-but-set-variable]
  int i = 0, n = 0;
             ^
/myMPD/dist/mjson/mjson.c:743:17: error: variable 'n' set but not used [-Werror,-Wunused-but-set-variable]
  int sign = 1, n = 0;

haproxy-mirror pushed a commit to haproxy/haproxy that referenced this issue May 11, 2023
clang 15 reports unused variables in src/mjson.c:

  src/mjson.c:196:21: fatal error: expected ';' at end of declaration
    int __maybe_unused n = 0;

and

  src/mjson.c:727:17: fatal error: variable 'n' set but not used [-Wunused-but-set-variable]
    int sign = 1, n = 0;

An issue was created on the project, but it was not fixed for now:

   cesanta/mjson#51

So for now, to fix the build issue, these variables are declared as unused.
Of course, if there is any update on this library, be careful to review this
patch first to be sure it is always required.

This patch should fix the issue #1868. It be backported as far as 2.4.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants