Author Topic: engineering ethics (it was [SOLVED] funny CC)  (Read 35838 times)

0 Members and 1 Guest are viewing this topic.

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
engineering ethics (it was [SOLVED] funny CC)
« on: November 20, 2015, 03:45:21 pm »
yesterday I was hammering my head with a piece of C code (not written by me, & bare metal profiled for MIPS32) which continuously crashes when compiled with SierraC while it works when compiled with gcc

Code: [Select]
        while (p_btree->context.queue isNotEqualTo NULL)
        {
            p_node = queue_de(p_btree);
            if ((p_node->parent isNotEqualTo NULL)logicalAnd(p_node isEqualTo p_node->parent->pointer[0]))
            {
                rank_new = tree_get_path_lengh_to_root(p_root, p_node);
                if (rank_new isNotEqualTo rank)
                {
                    rank = rank_new;
                }
            }
        }
working with gcc, not working with sierraC

Code: [Select]
        is_done=(p_btree->context.queue isEqualTo NULL);
        while (is_done isNotEqualTo True)
        {
            p_node = queue_de(p_btree);           
            is_ok1=(p_node->parent isNotEqualTo NULL);
            if (is_ok1 isEqualTo True)
            {
                is_ok2=(p_node isEqualTo p_node->parent->pointer[0]);
                if (is_ok2 isEqualTo True)
                {
                   rank2 = tree_get_path_lengh_to_root(p_root, p_node);
                   if (rank2 isNotEqualTo rank1)
                   {
                       rank1 = rank2;
                   }
                }
            }
            is_done=(p_btree->context.queue isEqualTo NULL);
        }
working with both sierraC and gcc


of course the "fixed" version is too pedantic, but just a proof of concept, in order to understand WTF is wrong in the original code
and as far as I understand WTF happened, my debug TAP has found that the problem is around the following line
Code: [Select]
if ((p_node->parent isNotEqualTo NULL)logicalAnd(p_node isEqualTo p_node->parent->pointer[0]))

more specifically (analyzing the assembly code) it seems that it depends by the CC implementation

SierraC seems to implement the IF-STATEMENT as
  • step1: eval (p_node->parent isNotEqualTo NULL)
  • step2: eval (p_node isEqualTo p_node->parent->pointer[0])
  • step3: if they are both True then take the IF branch, else take the ELSE branch

GCC seems to implement the IF-STATEMENT as
  • step1: eval (p_node->parent isNotEqualTo NULL)
  • step2: if the previous eval is True then eval (p_node isEqualTo p_node->parent->pointer[0])
  • step3: if (is_ok2 isEqualTo True) then take the IF branch, else take the ELSE branch

so the problem with SierraC is … with "p_node->parent->pointer" which crashes when "p_node->parent" is NULL


if so, it means that I have 4K lines of C to be fixed :palm: :palm: :palm: :palm: :palm:
« Last Edit: November 22, 2015, 01:28:09 pm by legacy »
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: funny CC
« Reply #1 on: November 20, 2015, 04:11:52 pm »
You may want to check for the compiler switch for the "short-circuit" evaluation.
 

Offline 0xdeadbeef

  • Super Contributor
  • ***
  • Posts: 1577
  • Country: de
Re: funny CC
« Reply #2 on: November 20, 2015, 04:16:08 pm »
Sure the "logicalAnd" is defined the same in both versions?
I'm pretty sure that "&&" should always be short circuit according to the standard while "&" is not.
If both versions use "&&", there must be a compiler switch to ignore the standard.
Well, or the compiler is crap.
Trying is the first step towards failure - Homer J. Simpson
 

Offline grumpydoc

  • Super Contributor
  • ***
  • Posts: 2905
  • Country: gb
Re: funny CC
« Reply #3 on: November 20, 2015, 04:24:49 pm »
isNotEqualTo, logicalAnd etc - just use the *&^%!!! language FFS  :palm:

These sort of shenanigans do not, in general, help write bug free code.
 

Online newbrain

  • Super Contributor
  • ***
  • Posts: 1719
  • Country: se
Re: funny CC
« Reply #4 on: November 20, 2015, 04:49:46 pm »
If logicalAnd is defined as && (this is really horrible, what's the purpose?), then Sierra C is not ANSI standard compliant.

From C89 standard (well, the draft...):
Quote
3.3.13 Logical AND operator
[...]
   Unlike the bitwise binary & operator, the && operator guarantees
left-to-right evaluation; there is a sequence point after the
evaluation of the first operand.  If the first operand compares equal
to 0, the second operand is not evaluated.
The sequence point and definition guarantee that the second operand:
  • will only be evaluated if the first one is non-zero
  • its (possible) evaluation will happen only when all the side effects of the first one are completed

K&R C, if my memory helps, did not mandate short-circuit evaluation.
I'm not acquainted with Sierra C, there are almost no traces of it on the web, but it might have a switch to make it ANSI compliant.
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5319
  • Country: gb
Re: funny CC
« Reply #5 on: November 20, 2015, 05:12:40 pm »
isNotEqualTo, logicalAnd etc - just use the *&^%!!! language FFS  :palm:

These sort of shenanigans do not, in general, help write bug free code.

Pretty much my thoughts entirely, at the very least the code is much less readable.

I guess it's to save people from themselves on = vs == and the difference between bitwises and logicals. Any compiler worth its salt would offer a warning, assuming the dev hadn't tried to disable warnings that is.
 

Offline grumpydoc

  • Super Contributor
  • ***
  • Posts: 2905
  • Country: gb
Re: funny CC
« Reply #6 on: November 20, 2015, 05:16:33 pm »
Quote
K&R C, if my memory helps, did not mandate short-circuit evaluation.
My copy of K&R clearly states that && and || stop evaluation as soon as the value of the whole expression is known.
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: funny CC
« Reply #7 on: November 20, 2015, 05:25:21 pm »
Sure the "logicalAnd" is defined the same in both versions?

it's the alias for "&&"

Code: [Select]
#define logicalAnd     &&

I had to use the alias for an internal reason (my boss wants this way)

Well, or the compiler is crap.

 :-//
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: funny CC
« Reply #8 on: November 20, 2015, 05:32:28 pm »
If logicalAnd is defined as &&

yes it is

this is really horrible, what's the purpose?

it's more horrible to see "&&", which can be confused with "&"

then Sierra C is not ANSI standard compliant.

I am really afraid they are not C89

If the first operand compares equal to 0, the second operand is not evaluated.

exactly what happens  :palm:

K&R C, if my memory helps, did not mandate short-circuit evaluation.

So it might be a pre-ANSI-C compiler, or probably they have give me the wrong compiler version
or it's not configured in the right way (missing FLAGS? missing external profile that "enables/disables" features? who knows)
 

Online newbrain

  • Super Contributor
  • ***
  • Posts: 1719
  • Country: se
Re: funny CC
« Reply #9 on: November 20, 2015, 05:45:08 pm »
Quote
K&R C, if my memory helps, did not mandate short-circuit evaluation.
My copy of K&R clearly states that && and || stop evaluation as soon as the value of the whole expression is known.
I stand corrected :-X, unless that copy is the "Second Edition" (that is, already ANSI). My original one is long lost :(

Quote
it's more horrible to see "&&", which can be confused with "&"
To each their own. I do not agree in the least, but if you are fine with that :-//
Code: [Select]
#define isAssignedTheFollowingExpression =
The latest thing I could find about SierraC is a 1993 reference...so, well in the C89 domain (but still a number of non-compliant compiler might still be around, and you might have an older version...). Check the documentation for C89/ANSI compliance, or at least short-circuit evaluation.
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline Godzil

  • Frequent Contributor
  • **
  • Posts: 458
  • Country: fr
    • My own blog
Re: funny CC
« Reply #10 on: November 20, 2015, 05:47:25 pm »
sierraC, are you building for 68000 target? This was used by TI for there 68K based calculator, and the resulted assembly was just absolutely horrible..
When you make hardware without taking into account the needs of the eventual software developers, you end up with bloated hardware full of pointless excess. From the outset one must consider design from both a hardware and software perspective.
-- Yokoi Gunpei
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: funny CC
« Reply #11 on: November 20, 2015, 05:51:49 pm »
I guess it's to save people from themselves on = vs == and the difference between bitwises and logicals.
Any compiler worth its salt would offer a warning, assuming the dev hadn't tried to disable warnings that is.

emmm, SierraC does not offer a warning when you write "if (a=0) {}" instead of "if (a==0) {}"
I can agree or disagree, however it's imposed by our customers (more specifically by their QA dudes)
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: funny CC
« Reply #12 on: November 20, 2015, 06:01:00 pm »
sierraC, are you building for 68000 target?

yes, but it's not the SierraC Compiler that was bought by TI and used for their calculators
I can say that because it supports CPU32 (683xx) which was not supported by the TI version

here I have a few 683xx cores, and a DOS16 CC (it works under WinXP, or inside a virtual machine)
the full toolchain has been provided by a few customers who want to support their old equipment
I have suggested them a full port to PIC32, but they said that they can't change the baseline until their EOL
(avionics products, they are crazy)
 

Offline Godzil

  • Frequent Contributor
  • **
  • Posts: 458
  • Country: fr
    • My own blog
Re: funny CC
« Reply #13 on: November 20, 2015, 06:05:06 pm »
Avionics that use that crap compiler?!?!? O_O
When you make hardware without taking into account the needs of the eventual software developers, you end up with bloated hardware full of pointless excess. From the outset one must consider design from both a hardware and software perspective.
-- Yokoi Gunpei
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: funny CC
« Reply #14 on: November 20, 2015, 06:16:23 pm »
* * * solution found * * *

there is a profile file (.bat?) in the bin path which has the following line
Code: [Select]
SET  ev_shortcircuit 0

I have changed into
Code: [Select]
SET  ev_shortcircuit 1

and everything is now working as expected :D :D :D :D



so, it seems that it's really possible to "disable" that feature
too amazing  :-DD
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: funny CC
« Reply #15 on: November 20, 2015, 06:34:25 pm »
emmm, SierraC does not offer a warning when

when you have this set

Code: [Select]
SET  ev_wall 0

I have changed into

Code: [Select]
SET  ev_wall 1

and now it offers a lot of useful warnings  :D


conclusion: the environment was not configured in the proper way!
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: funny CC
« Reply #16 on: November 20, 2015, 06:39:53 pm »
I had to use the alias for an internal reason (my boss wants this way)
 :-//
Apparently your boss does not know C, but still wants to read it.
Send him on C-bootcamp or something.  :P
 

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 19517
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: funny CC
« Reply #17 on: November 20, 2015, 06:42:54 pm »
I had to use the alias for an internal reason (my boss wants this way)

Why don't you suggest also using
#define begin {
#define end }

Of course, if your boss cannot expain why they want something, then you shold get another boss.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12863
Re: [SOLVED] funny CC
« Reply #18 on: November 20, 2015, 07:04:04 pm »
Lets go the whole hog!
Code: [Select]
#define if if(
#define then )
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: funny CC
« Reply #19 on: November 20, 2015, 07:57:22 pm »
Apparently your boss does not know C

he is a pro ADA dude, probably too Pascal addicted  :-//
 

Offline richardman

  • Frequent Contributor
  • **
  • Posts: 427
  • Country: us
Re: [SOLVED] funny CC
« Reply #20 on: November 20, 2015, 08:10:53 pm »
You have a broken compiler and a broken boss. This could not be good for your career.
// richard http://imagecraft.com/
JumpStart C++ for Cortex (compiler/IDE/debugger): the fastest easiest way to get productive on Cortex-M.
Smart.IO: phone App for embedded systems with no app or wireless coding
 

Offline legacyTopic starter

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: [SOLVED] funny CC
« Reply #21 on: November 20, 2015, 08:11:03 pm »
cannot expain why

and can you explain why MISRA-C must be applied ?

e.g.
why on the why do I have to do a lot of "code refactoring" (which is extremely boring) in order to
1) replace multiple "return" with just one "return" at the end of the function body ?
2) remove "break" and "continue" ?

my boss, our customers, fanny people in avionics
but yes, if you can employ me, I will change my job  :D
« Last Edit: November 20, 2015, 08:25:18 pm by legacy »
 

Offline grumpydoc

  • Super Contributor
  • ***
  • Posts: 2905
  • Country: gb
Re: [SOLVED] funny CC
« Reply #22 on: November 20, 2015, 08:16:07 pm »
Quote
I stand corrected :-X, unless that copy is the "Second Edition" (that is, already ANSI). My original one is long lost :(
1st edition, bought 1986 according to the note I pot on the fly sheet. I had an earlier printing but mislaid it.

Quote
emmm, SierraC does not offer a warning when you write "if (a=0) {}" instead of "if (a==0) {}"
You can always use the "if (0==a){}" trick.

Quote
I can agree or disagree, however it's imposed by our customers (more specifically by their QA dudes)
Well, whoever pays the piper calls the tune I suppose.
 

Offline djacobow

  • Super Contributor
  • ***
  • Posts: 1151
  • Country: us
  • takin' it apart since the 70's
Re: [SOLVED] funny CC
« Reply #23 on: November 20, 2015, 08:19:15 pm »
I don't know why I feel the need to chime in, but not only is

        while (p_btree->context.queue isNotEqualTo NULL) { ... }

barfalicious, but even

        while(p_btree->context.queue != NULL) { ... } is awful

This is more idiomatic c:

        while(p_btree->context.queue) { ... }

In fact, I never compare against NULL or 0, and even when assigning a null pointer, I prefer to just use a 0, which is what NULL is defined as, anyway, unless a maniac did something else.

In c++11, nullptr solves the annoying problem of overloaded functions that might take a pointer or an integer, so I'd use that in that circumstance.

 

Offline Godzil

  • Frequent Contributor
  • **
  • Posts: 458
  • Country: fr
    • My own blog
Re: [SOLVED] funny CC
« Reply #24 on: November 20, 2015, 08:52:20 pm »
You are all thinking about 'normal" engineering, here it's related to avionic, where EVERYTHING need to be proved and done explicitely.

That's doesn't surprise me that the coding standard ask to use keyword instead of "==" or "&&" just to prevent any possible error, that does not surprise me that you HAVE to explicitly check for "while(xxx notEqual NULL)" instead of "while(xxx)"

As pointed, look at the MISRA-C reference, and find people that work on avionic software development, they are really strict on the coding standard.
When you make hardware without taking into account the needs of the eventual software developers, you end up with bloated hardware full of pointless excess. From the outset one must consider design from both a hardware and software perspective.
-- Yokoi Gunpei
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf