home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 3670 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.4 KB  |  154 lines

  1. Path: news.daimi.aau.dk!liborius
  2. From: liborius@daimi.aau.dk (Per Liboriussen)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: 3 bugs in SAS/C v. 6.56
  5. Date: 23 Feb 1996 09:04:44 GMT
  6. Organization: DAIMI, Computer Science Dept. at Aarhus University
  7. Message-ID: <4gjvvc$a3t@gjallar.daimi.aau.dk>
  8. NNTP-Posting-Host: lire.daimi.aau.dk
  9. Keywords: SAS/C,bug
  10.  
  11. I have found 3 bugs in SAS/C version 6.56: Two cases of generating bad code,
  12. and one case of refusing to compile a correct program. Below are descriptions
  13. of the problems, including the smallest programs I have been able to find
  14. which shows the bugs, and the commandlines used to compile. Hopefully someone
  15. from SAS reads this (and puts out a 6.57 patch ;-) ).
  16.  
  17. -------------------------------------------------------------------------
  18. Number 1:
  19.  
  20. The following program produces the output "bad" instead of "ok" as it should.
  21.  
  22. ------------------------------
  23. #include <stdio.h>
  24.  
  25. int main(void)
  26. {
  27.     unsigned short a = 0xffff, b = 0xffff;
  28.  
  29.     if ((a | b) > 0x7fff) {
  30.         printf("ok\n");
  31.     } else {
  32.         printf("bad\n");
  33.     }
  34.     return 0;
  35. }
  36. ------------------------------
  37.  
  38. This is the commandline and compiler output:
  39.   1> sc RESETOPTIONS ushort.c LINK
  40.   SAS/C Amiga Compiler 6.56
  41.   Copyright (c) 1988-1995 SAS Institute Inc.
  42.   Slink - Version 6.56
  43.   Copyright (c) 1988-1995 SAS Institute, Inc.  All Rights Reserved.
  44.   
  45.   
  46.   SLINK Complete - Maximum code size = 5392 ($00001510) bytes
  47.   
  48.   Final output file size = 5408 ($00001520) bytes
  49.   1>
  50.  
  51. The bitwise operation in the if-expression is important. If it is written
  52. simply as "if (a > 0x...)" it works correctly.
  53.  
  54. -------------------------------------------------------------------------
  55. Number 2:
  56.  
  57. This program causes the compiler to choke:
  58.  
  59. ------------------------------
  60. union un {
  61.     const char *cp;
  62.     char *p;
  63. };
  64.  
  65. const char *foo(union un *unp, int n)
  66. {
  67.     /* The following line is important. It seems that a conditional
  68.        expression involving both unp->cp and unp->p must appear somewhere
  69.        in order to trigger the bug. */
  70.     return (n ? unp->p : unp->cp);
  71. }
  72.  
  73. void bar(union un *unp)
  74. {
  75.     *(unp->p) = '7';
  76. }
  77. ------------------------------
  78.  
  79. The compiler output:
  80.   1> sc RESETOPTIONS const.c
  81.   SAS/C Amiga Compiler 6.56
  82.   Copyright (c) 1988-1995 SAS Institute Inc.
  83.   
  84.   ====================
  85.       *(unp->p) = '7';
  86.   const.c 16 Error 99: attempt to change a const value
  87.   1>
  88.  
  89. As the comment in foo() indicates, the conditional expression is important.
  90. If it is removed, or even if foo() is rewritten to return a char and the line
  91. changed to "return (n ? *unp->p : *unp->cp);", the bug goes away.
  92.  
  93. -------------------------------------------------------------------------
  94. Number 3:
  95.  
  96. At least this one shows itself more clearly and dramatically than number 1. ;-)
  97.  
  98. ------------------------------
  99. void foo(float);
  100. void bar(float);
  101.  
  102. void foo(f)
  103. float f;
  104. {
  105.     bar(f);
  106. }
  107. ------------------------------
  108.  
  109. When compiled with
  110.   1> sc RESETOPTIONS PARAMETERS=REGISTER NOSTACKCHECK float.c
  111.   SAS/C Amiga Compiler 6.56
  112.   Copyright (c) 1988-1995 SAS Institute Inc.
  113.   1>
  114.  
  115. it produces the following assembler output: (the NOSTACKCHECK option is not
  116. important, it is just to reduce the clutter in the asm. file)
  117.  
  118. ------------------------------
  119. SAS AMIGA 680x0OBJ Module Disassembler 6.55
  120. Copyright ⌐ 1995 SAS Institute, Inc.
  121.  
  122.  
  123. Amiga Object File Loader V1.00
  124. 68000 Instruction Set
  125.  
  126. EXTERNAL DEFINITIONS
  127.  
  128. @foo 0000-00
  129.  
  130. SECTION 00 "text" 00000010 BYTES
  131.        | 0000  594F                           SUBQ.W      #4,A7
  132.        | 0002  48D7 0003                      MOVEM.L     D0-D1,(A7)
  133.        | 0006  2017                           MOVE.L      (A7),D0
  134.        | 0008  6100  0000-XX.1                BSR.W       @bar
  135.        | 000C  584F                           ADDQ.W      #4,A7
  136.        | 000E  4E75                           RTS
  137. ------------------------------
  138.  
  139. The two first assembler instructions shows the problem: the compiler allocates
  140. room for four bytes on the stack and promptly writes eight bytes to the
  141. location, thus owerwriting the return address for the procedure.
  142.  
  143. This one might actually be less of a bug than the two other, because I think
  144. the code in the example is broken. By providing a prototype for foo() and then
  145. defining it old-style with a float argument, I believe it invokes undefined
  146. behaviour. However, I still think it is naughty of the compiler to overwrite
  147. the return address ;-).
  148.  
  149.  
  150. --
  151. Per Liboriussen
  152. liborius@daimi.aau.dk
  153.  
  154.