bugTiny C Compiler - Bugs: bug #51169, tcc miscompiles unsigned long long...

 
 

bug #51169: tcc miscompiles unsigned long long bitfields

Submitter:  Tony Garnock-Jones <surazal>
Submitted:  Sat 03 Jun 2017 03:04:50 PM UTC
   
 
Category:  None Severity:  3 - Normal
Priority:  5 - Normal Item Group:  None
Status:  None Privacy:  Public
Assigned to:  None Open/Closed:  Open
* Mandatory Fields

Post a Comment

Add a New Comment Rich Markup
   

Discussion

Sat 03 Jun 2017 05:00:50 PM UTC, comment #1: 

Could the following patch be a step toward a fix? (Also attached)


diff --git a/tccgen.c b/tccgen.c
index 1a8ba3d..68fcd19 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -5750,11 +5750,13 @@ static void block(int *bsym, int *csym, int is_expr)
         for (b = 1; b < sw.n; b++)
             if (sw.p[b - 1]->v2 >= sw.p[b]->v1)
                 tcc_error("duplicate case value");
+        vpushv(&switchval);
         /* Our switch table sorting is signed, so the compared
            value needs to be as well when it's 64bit.  */
-        if ((switchval.type.t & VT_BTYPE) == VT_LLONG)
-            switchval.type.t &= ~VT_UNSIGNED;
-        vpushv(&switchval);
+        if ((switchval.type.t & VT_BTYPE) == VT_LLONG) {
+            switchval.type.t = VT_LLONG;
+            gen_cast(&switchval.type);
+        }
         gcase(sw.p, sw.n, &a);
         vpop();
         if (sw.def_sym)


Tony Garnock-Jones <surazal>
Sat 03 Jun 2017 03:04:50 PM UTC, original submission:  

This is using tcc:i386 on Debian,
tcc version 0.9.27 (prerelease) (i386 Linux)

The following program should produce exit status 0, but produces exit status 1.

The problem is that it is using SAR instead of SHR to extract the bit field in the switch.

struct foo {
  unsigned long long t: 4;
  unsigned long long s: 60;
};

void init_foo(struct foo *f) {
  f->t = 8;
  f->s = 0;
}

int main(int argc, char *argv[]) {
  struct foo f;
  init_foo(&f);
  switch (f.t) {
    case 8:
      return 0;
    default:
      return 1;
  }
}

Tony Garnock-Jones <surazal>

 

Attached Files

Attached Files
file #40848:  t.c added by surazal (277B - text/x-csrc)

(Note: upload size limit is set to 4.0MiB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

 

Dependencies

This item does not depend on any other items.

No items depend on this one.

 

Mail Notification Carbon-Copy List

Carbon-Copy List
  • -email is unavailable- added by surazal (Submitted the item)
  •  

    Votes

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    History

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2017-06-03 surazal Attached File- Added experimental-bug51169.patch, #40849
    2017-06-03 surazal Attached File- Added t.c, #40848

    Back to the top

    Powered by Savane 3.16-ed84.
    Corresponding source code