News for hackers. Stuff that matters.
Re:JPL Institutional Coding Standard for the C Programming L... by carlmr on Monday February 18, 2019 @03:42AM (#19189361)
MISRA C is annoying mostly if you have to change the codebase to fit the standard. If you iteratively solve MISRA warnings they become second nature and you almost don't make them anymore.

What is annoying is the paperwork when you need a deviation. However that's kind of the point of the process. Make it annoying enough that people really think about whether the deviation is necessary.

At the same time it's only a coding standard. Not a panacea for all your coding issues. You can have 100% MISRA conformant spaghetti.

It does reduce the "shoot yourself in the foot" surface area a little bit though.

Re:JPL Institutional Coding Standard for the C Programming L... by acprog42 on Monday February 18, 2019 @04:05AM (#19189442)
> You can have 100% MISRA conformant spaghetti.

IMHO some of MISRA rules lead directly to hard to read/maintain code. Consider a function that:

1. opens a file 2. allocates enough memory to store the whole file 3. reads the file 4. return the allocated buffer on success or NULL on failure

You want to write it so it doesn't leak either memory or file handles whether successful or not (if successful ownership of the memory buffer is passed to the caller so it must not be freed in that case).

To be MISRA compliant you'd either end up with a "Christmas tree" of nested scopes or if-statements with extra && in them (pseudo C-code):

    char *buf = NULL;
    FILE *fh = fopen(...);
    if (fh) {
      if (success(fseek(fh, end))) {
        long int sz = ftell(fh);
        if (sz > 0) {
          buf = malloc(sz);
          if (buf) {
            if (failed(fread(fh, buf))) {
              report_error();
              free(buf);
              buf = NULL;
            }
          } else {
            report_error();
          }
        } else {
          report_error();
        }
      } else {
        report_error();
      }
      fclose(fh);
    } else {
      report_error();
    }
    return buf;
However if you were allowed to use goto with a single exit-label the code would be much cleaner and easier to follow:

    char *buf = NULL;
    char *rv = NULL;
    FILE *fh = fopen(...);
    if (!fh) {
      report_error();
      goto exit;
    }
    if (fail(fseek(fh, end))) {
      report_error();
      goto exit;
    }
    long int sz = ftell(fh);
    if (sz <= 0) {
      report_error();
      goto exit;
    }
    buf = malloc(sz);
    if (!buf) {
      report_error();
      goto exit;
    }
    if (failed(fread(fh, buf))) {
      report_error();
      goto exit;
    }
    rv = buf;
    buf = NULL;
exit: if (fh) { fclose(fh); } if (buf) { free(buf); } return rv;
Re:JPL Institutional Coding Standard for the C Programming L... by kelvich on Monday February 18, 2019 @06:33AM (#19190008)
Actually you can do that without goto's, by using do {} while loop and break:

    FILE *fh;
    char *buf = NULL;
    char *rv = NULL;

    do {
        fh = fopen(...);
        if (!fh) {
            report_error();
            break;
        }

        if (fail(fseek(fh, end))) {
            report_error();
            break;
        }

        long int sz = ftell(fh);
        if (sz <= 0) {
            report_error();
            break;
        }

        buf = malloc(sz);
        if (!buf) {
            report_error();
            break;
        }
        if (failed(fread(fh, buf))) {
            report_error();
            break;
        }

        rv = buf;
        buf = NULL;
    } while(false);

    if (fh)
        fclose(fh);
    
    if (buf)
        free(buf);
    
    return rv;
Re:JPL Institutional Coding Standard for the C Programming L... by flyinglizard on Monday February 18, 2019 @09:40AM (#19190848)
Actually it’s not the same, as using goto lets you have very natural reverse order cleanup at the exit of the function (widely used in the Linux kernel for example).
Re:JPL Institutional Coding Standard for the C Programming L... by acprog42 on Monday February 18, 2019 @07:12AM (#19190137)
Sure, that would pass the `grep -r goto ` test, but is just a more convoluted way of doing the same thing.
Re:JPL Institutional Coding Standard for the C Programming L... by coldtea on Monday February 18, 2019 @10:18AM (#19191228)
That's fine: the intention was not to do something else, but to avoid goto while still doing the same functionality.

Plus, it might be slightly more convoluted than goto, but is less convoluted than the original over-nested code, which was the intention.

Re:JPL Institutional Coding Standard for the C Programming L... by acprog42 on Monday February 18, 2019 @10:55AM (#19191546)
Which brings us back full circle to the point I was originally trying to convey: if a coding standard forces you to write more convoluted and harder-to-read code just to work around some of its rules, that is a clear failure of said standard.

Fortunately MISRA seems to have gotten back some sanity in the 2012 revision compared to the one the above JPL standard is based on.

Re:JPL Institutional Coding Standard for the C Programming L... by coldtea on Monday February 18, 2019 @11:40AM (#19191906)
>Which brings us back full circle to the point I was originally trying to convey: if a coding standard forces you to write more convoluted and harder-to-read code just to work around some of its rules, that is a clear failure of said standard.

Only if the "more convoluted and harder-to-read code" is worse than what the standard tries to avoid.

A standard that calls for not allocating memory dynamically for example might result in less elegant code than one that does, for example, but that's not a "clear failure" since that away it avoids the uncertainty and variable runtime performance that malloc brings.

I'm addressing the general claim here, that "if a coding standard forces you to write more convoluted and harder-to-read code just to work around some of its rules, that is a clear failure of said standard" -- not specifically where the particular no-goto rule was justified.

A standard can be perfectly valid and good in restricting things even if that forces programmers to write "less elegant code" -- as long as this is necessary to fulfil some other objective of the standard (e.g. easy formal verification or real-time behavior).

Re:JPL Institutional Coding Standard for the C Programming L... by vbezhenar on Monday February 18, 2019 @10:27AM (#19191309)
You must prove that this loop will terminate: All loops shall have a statically determinable upper-bound on the maximum number of loop iterations. It shall be possible for a static compliance checking tool to affirm the existence of the bound.
Re:JPL Institutional Coding Standard for the C Programming L... by TheCoelacanth on Monday February 18, 2019 @10:39AM (#19191411)
Since the loop condition is the constant `false`, it should be trivial to prove that the upper bound is one iteration.
Re:JPL Institutional Coding Standard for the C Programming L... by konstmonst on Monday February 18, 2019 @07:45AM (#19190250)
Which muddles the intent and is worse than using goto imho
Re:JPL Institutional Coding Standard for the C Programming L... by thom on Monday February 18, 2019 @07:56AM (#19190310)
I assumed the intent was to suggest a similar structure that wouldn't raise warnings under one of these sets of coding standards.
Re:JPL Institutional Coding Standard for the C Programming L... by acprog42 on Monday February 18, 2019 @04:18AM (#19189485)
Too little karma to edit and fix indentation. Anyway given the above functions, think about adding support for only returning the buffer if the file contains a specific word. While certainly doable in both cases I would at least feel more uncertain editing the first without accidentally creating a bug...
Re:JPL Institutional Coding Standard for the C Programming L... by philpem on Monday February 18, 2019 @11:17AM (#19191727)
This is actually one of the specific variations to the "don't use GOTO" rule in MISRA-C-2012.

You're allowed to use GOTO provided you're jumping forward, and only for error handling.

Re:JPL Institutional Coding Standard for the C Programming L... by carlmr on Monday February 18, 2019 @05:37AM (#19189815)
I completely agree. Most Misra software is embedded without dynamic allocation though, so you usually don't get into this situation too often.

I think an exception here makes sense

Re:JPL Institutional Coding Standard for the C Programming L... by acprog42 on Monday February 18, 2019 @06:19AM (#19189969)
> Most Misra software is embedded without dynamic allocation though

True, but I've seen enough MISRA code bases plagued with the "Christmas tree" layout even if dynamic memory allocation isn't allowed. This was just a generic example most people can relate to.

Re:JPL Institutional Coding Standard for the C Programming L... by carlmr on Monday February 18, 2019 @06:30AM (#19189997)
I just checked, and MISRA 2012 seems to allow goto again under certain preconditions (you have to have a label declared in the same function and it has to be in the same block). So actually you can do proper error handling again.

Still single return statement, which usually makes for more spaghetti.

Re:JPL Institutional Coding Standard for the C Programming L... by acprog42 on Monday February 18, 2019 @07:22AM (#19190166)
Yeah, seems like it actually allows my second example which is good! :)
Re:JPL Institutional Coding Standard for the C Programming L... by regularfry on Monday February 18, 2019 @05:32AM (#19189794)
If you allowed yourself a second label, you could clean up those repeated `report_error()` calls, too.
Re:JPL Institutional Coding Standard for the C Programming L... by acprog42 on Monday February 18, 2019 @06:15AM (#19189955)
True, it would be kind of analogue to try/except/finally if you do that.
Re:JPL Institutional Coding Standard for the C Programming L... by Jedi72 on Monday February 18, 2019 @05:34AM (#19189804)
I'm so glad I don't use C hahaha. Y'all are crazy!
Re:JPL Institutional Coding Standard for the C Programming L... by philpem on Monday February 18, 2019 @11:16AM (#19191721)
Definitely agree here.

Shoehorning an existing codebase into being MISRA compliant is utter hell. It's something you really need to design-in from the beginning.

Also for anyone in the audience who's going "ugh, MISRA..." -- the 2012 spec revision is a significant improvement on the 2004 version and cleans up a lot of the more troublesome rules.

Even if I'm not doing MISRA-required code, I still find myself sticking to its suggestions... "Allocate memory statically" is a really good one, especially on embedded systems.