Menu

Strange : Zinc handling of comparison to nil

Help
Hibou57
2008-01-30
2013-04-26
  • Hibou57

    Hibou57 - 2008-01-30

    Halo,

    I've meet a bug with Zinc. A source written with "if xxx == nil" comit a crash, while writing
    "if is nil (xxx)" is ok.

    Here is an exemple of what Zinc do when using "is nil (xxx)" :

    -------------------------------------------------------------------------------------------
    | if(((void*)_decoded_ustring_7==(void*)0))
    | {
    | _code_3=3;
    | }
    -------------------------------------------------------------------------------------------

    And now here is what is do with "xxx == nil"

    -------------------------------------------------------------------------------------------
    |if((_compare_p_a_wo_p_a_wo(_decoded_ustring_7,((unsigned short *)0))==0))
    |{
    |_code_3=3;
    |}
    -------------------------------------------------------------------------------------------

    The first is called as a function (is nil), but is written inline, as we would except "xxx == nil" to be written. And if we write "xxx == nil", it calls a function, try to access a memory area pointed to by a nul pointer.... and then the crash.

    The code which is called when "xxx == nil" occurs in a Zinc source code:
    -------------------------------------------------------------------------------------------
    |static int _compare_p_a_wo_p_a_wo(unsigned short *_s1, unsigned short *_s2)
    |{
    |unsigned short _c_1;
    |while(1)
    |{
    |_c_1=(_s1[0]);
    |if((_c_1!=(_s2[0])))
    |{
    |break;
    |}
    |if((_c_1==((unsigned short)0)))
    |{
    |break;
    |}
    |(++_s1);
    |(++_s2);
    |}
    |return ((int )((char )(_c_1-(_s2[0]))));
    |}
    -------------------------------------------------------------------------------------------

    Strange behaviour... isn't it ?

     
    • Marc Kerbiquet

      Marc Kerbiquet - 2008-01-30

      It's not a bug, it's a feature ;-)

      'is nil (x)' is a builtin function to check a null pointer.

      But the '==' operator is overridable just like in C++ and is overridden
      for the string type in string.zc:

        public equ @equal (s1: string, s2: string) = compare (s1, s2) == 0

       
    • Hibou57

      Hibou57 - 2008-02-01

      Is there a built in function to allocate memory on the stack ?

       
    • Marc Kerbiquet

      Marc Kerbiquet - 2008-02-01

      Do you mean a alloca() C equivalent ? no there isn't, but it could be easily exposed (today, zinc just generates C source).

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.