(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
builtin-object-size-3.c
       1  /* { dg-do run } */
       2  /* { dg-options "-O2 -Wno-stringop-overread" } */
       3  /* { dg-require-effective-target alloca } */
       4  
       5  #include "builtin-object-size-common.h"
       6  
       7  struct A
       8  {
       9    char a[10];
      10    int b;
      11    char c[10];
      12  } y, w[4];
      13  
      14  extern char exta[];
      15  extern char extb[30];
      16  extern struct A zerol[0];
      17  
      18  void
      19  __attribute__ ((noinline))
      20  test1 (void *q, int x)
      21  {
      22    struct A a;
      23    void *p = &a.a[3], *r;
      24    char var[x + 10];
      25    if (x < 0)
      26      r = &a.a[9];
      27    else
      28      r = &a.c[1];
      29    if (__builtin_object_size (p, 2)
      30        != sizeof (a) - __builtin_offsetof (struct A, a) - 3)
      31      FAIL ();
      32    if (__builtin_object_size (&a.c[9], 2)
      33        != sizeof (a) - __builtin_offsetof (struct A, c) - 9)
      34      FAIL ();
      35    if (__builtin_object_size (q, 2) != 0)
      36      FAIL ();
      37  #ifdef __builtin_object_size
      38    if (__builtin_object_size (r, 2)
      39        != (x < 0
      40  	  ? sizeof (a) - __builtin_offsetof (struct A, a) - 9
      41  	  : sizeof (a) - __builtin_offsetof (struct A, c) - 1))
      42      FAIL ();
      43  #else
      44    if (__builtin_object_size (r, 2)
      45        != sizeof (a) - __builtin_offsetof (struct A, c) - 1)
      46      FAIL ();
      47  #endif
      48    if (x < 6)
      49      r = &w[2].a[1];
      50    else
      51      r = &a.a[6];
      52    if (__builtin_object_size (&y, 2)
      53        != sizeof (y))
      54      FAIL ();
      55    if (__builtin_object_size (w, 2)
      56        != sizeof (w))
      57      FAIL ();
      58    if (__builtin_object_size (&y.b, 2)
      59        != sizeof (a) - __builtin_offsetof (struct A, b))
      60      FAIL ();
      61  #ifdef __builtin_object_size
      62    if (__builtin_object_size (r, 2)
      63        != (x < 6
      64  	  ? 2 * sizeof (w[0]) - __builtin_offsetof (struct A, a) - 1
      65  	  : sizeof (a) - __builtin_offsetof (struct A, a) - 6))
      66      FAIL ();
      67  #else
      68    if (__builtin_object_size (r, 2)
      69        != sizeof (a) - __builtin_offsetof (struct A, a) - 6)
      70      FAIL ();
      71  #endif
      72    if (x < 20)
      73      r = malloc (30);
      74    else
      75      r = calloc (2, 16);
      76  #ifdef __builtin_object_size
      77    if (__builtin_object_size (r, 2) != (x < 20 ? 30 : 2 * 16))
      78      FAIL ();
      79  #else
      80    if (__builtin_object_size (r, 2) != 30)
      81      FAIL ();
      82  #endif
      83    if (x < 20)
      84      r = malloc (30);
      85    else
      86      r = calloc (2, 14);
      87  #ifdef __builtin_object_size
      88    if (__builtin_object_size (r, 2) != (x < 20 ? 30 : 2 * 14))
      89      FAIL ();
      90  #else
      91    if (__builtin_object_size (r, 2) != 2 * 14)
      92      FAIL ();
      93  #endif
      94    if (x < 30)
      95      r = malloc (sizeof (a));
      96    else
      97      r = &a.a[3];
      98  #ifdef __builtin_object_size
      99    size_t objsz = (x < 30 ? sizeof (a)
     100  		  : sizeof (a) - __builtin_offsetof (struct A, a) - 3);
     101    if (__builtin_object_size (r, 2) != objsz)
     102      FAIL ();
     103  #else
     104    if (__builtin_object_size (r, 2)
     105        != sizeof (a) - __builtin_offsetof (struct A, a) - 3)
     106      FAIL ();
     107  #endif
     108    r = memcpy (r, "a", 2);
     109  #ifdef __builtin_object_size
     110    if (__builtin_object_size (r, 2) != objsz)
     111      FAIL ();
     112  #else
     113    if (__builtin_object_size (r, 2)
     114        != sizeof (a) - __builtin_offsetof (struct A, a) - 3)
     115      FAIL ();
     116  #endif
     117    r = memcpy (r + 2, "b", 2) + 2;
     118  #ifdef __builtin_object_size
     119    if (__builtin_object_size (r, 2) != objsz - 4)
     120      FAIL ();
     121  #else
     122    if (__builtin_object_size (r, 2)
     123        != sizeof (a) - __builtin_offsetof (struct A, a) - 3 - 4)
     124      FAIL ();
     125  #endif
     126    r = &a.a[4];
     127    r = memset (r, 'a', 2);
     128    if (__builtin_object_size (r, 2)
     129        != sizeof (a) - __builtin_offsetof (struct A, a) - 4)
     130      FAIL ();
     131    r = memset (r + 2, 'b', 2) + 2;
     132    if (__builtin_object_size (r, 2)
     133        != sizeof (a) - __builtin_offsetof (struct A, a) - 8)
     134      FAIL ();
     135    r = &a.a[1];
     136    r = strcpy (r, "ab");
     137    if (__builtin_object_size (r, 2)
     138        != sizeof (a) - __builtin_offsetof (struct A, a) - 1)
     139      FAIL ();
     140    r = strcpy (r + 2, "cd") + 2;
     141    if (__builtin_object_size (r, 2)
     142        != sizeof (a) - __builtin_offsetof (struct A, a) - 5)
     143      FAIL ();
     144    if (__builtin_object_size (exta, 2) != 0)
     145      FAIL ();
     146    if (__builtin_object_size (exta + 10, 2) != 0)
     147      FAIL ();
     148    if (__builtin_object_size (&exta[5], 2) != 0)
     149      FAIL ();
     150    if (__builtin_object_size (extb, 2) != sizeof (extb))
     151      FAIL ();
     152    if (__builtin_object_size (extb + 10, 2) != sizeof (extb) - 10)
     153      FAIL ();
     154    if (__builtin_object_size (&extb[5], 2) != sizeof (extb) - 5)
     155      FAIL ();
     156  #ifdef __builtin_object_size
     157    if (__builtin_object_size (var, 2) != x + 10)
     158      FAIL ();
     159    if (__builtin_object_size (var + 10, 2) != x)
     160      FAIL ();
     161    if (__builtin_object_size (&var[5], 2) != x + 5)
     162      FAIL ();
     163  #else
     164    if (__builtin_object_size (var, 2) != 0)
     165      FAIL ();
     166    if (__builtin_object_size (var + 10, 2) != 0)
     167      FAIL ();
     168    if (__builtin_object_size (&var[5], 2) != 0)
     169      FAIL ();
     170  #endif
     171    if (__builtin_object_size (zerol, 2) != 0)
     172      FAIL ();
     173    if (__builtin_object_size (&zerol, 2) != 0)
     174      FAIL ();
     175    if (__builtin_object_size (&zerol[0], 2) != 0)
     176      FAIL ();
     177    if (__builtin_object_size (zerol[0].a, 2) != 0)
     178      FAIL ();
     179    if (__builtin_object_size (&zerol[0].a[0], 2) != 0)
     180      FAIL ();
     181    if (__builtin_object_size (&zerol[0].b, 2) != 0)
     182      FAIL ();
     183    if (__builtin_object_size ("abcdefg", 2) != sizeof ("abcdefg"))
     184      FAIL ();
     185    if (__builtin_object_size ("abcd\0efg", 2) != sizeof ("abcd\0efg"))
     186      FAIL ();
     187    if (__builtin_object_size (&"abcd\0efg", 2) != sizeof ("abcd\0efg"))
     188      FAIL ();
     189    if (__builtin_object_size (&"abcd\0efg"[0], 2) != sizeof ("abcd\0efg"))
     190      FAIL ();
     191    if (__builtin_object_size (&"abcd\0efg"[4], 2) != sizeof ("abcd\0efg") - 4)
     192      FAIL ();
     193    if (__builtin_object_size ("abcd\0efg" + 5, 2) != sizeof ("abcd\0efg") - 5)
     194      FAIL ();
     195    if (__builtin_object_size (L"abcdefg", 2) != sizeof (L"abcdefg"))
     196      FAIL ();
     197    r = (char *) L"abcd\0efg";
     198    if (__builtin_object_size (r + 2, 2) != sizeof (L"abcd\0efg") - 2)
     199      FAIL ();
     200  }
     201  
     202  size_t l1 = 1;
     203  
     204  void
     205  __attribute__ ((noinline))
     206  test2 (void)
     207  {
     208    struct B { char buf1[10]; char buf2[10]; } a;
     209    char *r, buf3[20];
     210    int i;
     211  #ifdef __builtin_object_size
     212    size_t dyn_res;
     213  #endif
     214  
     215    if (sizeof (a) != 20)
     216      return;
     217  
     218    r = buf3;
     219    for (i = 0; i < 4; ++i)
     220      {
     221        if (i == l1 - 1)
     222  	r = &a.buf1[1];
     223        else if (i == l1)
     224  	r = &a.buf2[7];
     225        else if (i == l1 + 1)
     226  	r = &buf3[5];
     227        else if (i == l1 + 2)
     228  	r = &a.buf1[9];
     229      }
     230  #ifdef __builtin_object_size
     231    dyn_res = sizeof (buf3);
     232  
     233    for (i = 0; i < 4; ++i)
     234      {
     235        if (i == l1 - 1)
     236  	dyn_res = sizeof (a) - __builtin_offsetof (struct B, buf1) - 1;
     237        else if (i == l1)
     238  	dyn_res = sizeof (a) - __builtin_offsetof (struct B, buf2) - 7;
     239        else if (i == l1 + 1)
     240  	dyn_res = sizeof (buf3) - 5;
     241        else if (i == l1 + 2)
     242  	dyn_res = sizeof (a) - __builtin_offsetof (struct B, buf1) - 9;
     243      }
     244    if (__builtin_object_size (r, 2) != dyn_res)
     245      FAIL ();
     246  #else
     247    if (__builtin_object_size (r, 2) != 3)
     248      FAIL ();
     249  #endif
     250    r = &buf3[20];
     251    for (i = 0; i < 4; ++i)
     252      {
     253        if (i == l1 - 1)
     254  	r = &a.buf1[7];
     255        else if (i == l1)
     256  	r = &a.buf2[7];
     257        else if (i == l1 + 1)
     258  	r = &buf3[5];
     259        else if (i == l1 + 2)
     260  	r = &a.buf1[9];
     261      }
     262    if (__builtin_object_size (r, 2) != 0)
     263      FAIL ();
     264    r = &buf3[2];
     265    for (i = 0; i < 4; ++i)
     266      {
     267        if (i == l1 - 1)
     268  	r = &a.buf1[1];
     269        else if (i == l1)
     270  	r = &a.buf1[2];
     271        else if (i == l1 + 1)
     272  	r = &buf3[5];
     273        else if (i == l1 + 2)
     274  	r = &a.buf1[4];
     275      }
     276  #ifdef __builtin_object_size
     277    dyn_res = sizeof (buf3) - 2;
     278  
     279    for (i = 0; i < 4; ++i)
     280      {
     281        if (i == l1 - 1)
     282  	dyn_res = sizeof (a) - __builtin_offsetof (struct B, buf1) - 1;
     283        else if (i == l1)
     284  	dyn_res = sizeof (a) - __builtin_offsetof (struct B, buf1) - 2;
     285        else if (i == l1 + 1)
     286  	dyn_res = sizeof (buf3) - 5;
     287        else if (i == l1 + 2)
     288  	dyn_res = sizeof (a) - __builtin_offsetof (struct B, buf1) - 4;
     289      }
     290    if (__builtin_object_size (r, 2) != dyn_res)
     291      FAIL ();
     292  #else
     293    if (__builtin_object_size (r, 2) != 15)
     294      FAIL ();
     295  #endif
     296    r += 8;
     297  #ifdef __builtin_object_size
     298    dyn_res -= 8;
     299    if (__builtin_object_size (r, 2) != dyn_res)
     300      FAIL ();
     301    if (dyn_res >= 6)
     302      {
     303        if (__builtin_object_size (r + 6, 2) != dyn_res - 6)
     304  	FAIL ();
     305      }
     306    else if (__builtin_object_size (r + 6, 2) != 0)
     307      FAIL ();
     308  #else
     309    if (__builtin_object_size (r, 2) != 7)
     310      FAIL ();
     311    if (__builtin_object_size (r + 6, 2) != 1)
     312      FAIL ();
     313  #endif
     314    r = &buf3[18];
     315    for (i = 0; i < 4; ++i)
     316      {
     317        if (i == l1 - 1)
     318  	r = &a.buf1[9];
     319        else if (i == l1)
     320  	r = &a.buf2[9];
     321        else if (i == l1 + 1)
     322  	r = &buf3[5];
     323        else if (i == l1 + 2)
     324  	r = &a.buf1[4];
     325      }
     326  #ifdef __builtin_object_size
     327    dyn_res = sizeof (buf3) - 18;
     328  
     329    for (i = 0; i < 4; ++i)
     330      {
     331        if (i == l1 - 1)
     332  	dyn_res = sizeof (a) - __builtin_offsetof (struct B, buf1) - 9;
     333        else if (i == l1)
     334  	dyn_res = sizeof (a) - __builtin_offsetof (struct B, buf2) - 9;
     335        else if (i == l1 + 1)
     336  	dyn_res = sizeof (buf3) - 5;
     337        else if (i == l1 + 2)
     338  	dyn_res = sizeof (a) - __builtin_offsetof (struct B, buf1) - 4;
     339      }
     340    if (dyn_res >= 12)
     341      {
     342        if (__builtin_object_size (r + 12, 2) != dyn_res - 12)
     343  	FAIL ();
     344      }
     345    else if (__builtin_object_size (r + 12, 2) != 0)
     346      FAIL ();
     347  #else
     348    if (__builtin_object_size (r + 12, 2) != 0)
     349      FAIL ();
     350  #endif
     351  }
     352  
     353  void
     354  __attribute__ ((noinline))
     355  test3 (void)
     356  {
     357    char buf4[10];
     358    struct B { struct A a[2]; struct A b; char c[4]; char d; double e;
     359  	     _Complex double f; } x;
     360    double y;
     361    _Complex double z;
     362    double *dp;
     363  
     364    if (__builtin_object_size (buf4, 2) != sizeof (buf4))
     365      FAIL ();
     366    if (__builtin_object_size (&buf4, 2) != sizeof (buf4))
     367      FAIL ();
     368    if (__builtin_object_size (&buf4[0], 2) != sizeof (buf4))
     369      FAIL ();
     370    if (__builtin_object_size (&buf4[1], 2) != sizeof (buf4) - 1)
     371      FAIL ();
     372    if (__builtin_object_size (&x, 2) != sizeof (x))
     373      FAIL ();
     374    if (__builtin_object_size (&x.a, 2) != sizeof (x))
     375      FAIL ();
     376    if (__builtin_object_size (&x.a[0], 2) != sizeof (x))
     377      FAIL ();
     378    if (__builtin_object_size (&x.a[0].a, 2) != sizeof (x))
     379      FAIL ();
     380    if (__builtin_object_size (&x.a[0].a[0], 2) != sizeof (x))
     381      FAIL ();
     382    if (__builtin_object_size (&x.a[0].a[3], 2) != sizeof (x) - 3)
     383      FAIL ();
     384    if (__builtin_object_size (&x.a[0].b, 2)
     385        != sizeof (x) - __builtin_offsetof (struct A, b))
     386      FAIL ();
     387    if (__builtin_object_size (&x.a[1].c, 2)
     388        != sizeof (x) - sizeof (struct A) - __builtin_offsetof (struct A, c))
     389      FAIL ();
     390    if (__builtin_object_size (&x.a[1].c[0], 2)
     391        != sizeof (x) - sizeof (struct A) - __builtin_offsetof (struct A, c))
     392      FAIL ();
     393    if (__builtin_object_size (&x.a[1].c[3], 2)
     394        != sizeof (x) - sizeof (struct A) - __builtin_offsetof (struct A, c) - 3)
     395      FAIL ();
     396    if (__builtin_object_size (&x.b, 2)
     397        != sizeof (x) - __builtin_offsetof (struct B, b))
     398      FAIL ();
     399    if (__builtin_object_size (&x.b.a, 2)
     400        != sizeof (x) - __builtin_offsetof (struct B, b))
     401      FAIL ();
     402    if (__builtin_object_size (&x.b.a[0], 2)
     403        != sizeof (x) - __builtin_offsetof (struct B, b))
     404      FAIL ();
     405    if (__builtin_object_size (&x.b.a[3], 2)
     406        != sizeof (x) - __builtin_offsetof (struct B, b) - 3)
     407      FAIL ();
     408    if (__builtin_object_size (&x.b.b, 2)
     409        != sizeof (x) - __builtin_offsetof (struct B, b)
     410  	 - __builtin_offsetof (struct A, b))
     411      FAIL ();
     412    if (__builtin_object_size (&x.b.c, 2)
     413        != sizeof (x) - __builtin_offsetof (struct B, b)
     414  	 - __builtin_offsetof (struct A, c))
     415      FAIL ();
     416    if (__builtin_object_size (&x.b.c[0], 2)
     417        != sizeof (x) - __builtin_offsetof (struct B, b)
     418  	 - __builtin_offsetof (struct A, c))
     419      FAIL ();
     420    if (__builtin_object_size (&x.b.c[3], 2)
     421        != sizeof (x) - __builtin_offsetof (struct B, b)
     422  	 - __builtin_offsetof (struct A, c) - 3)
     423      FAIL ();
     424    if (__builtin_object_size (&x.c, 2)
     425        != sizeof (x) - __builtin_offsetof (struct B, c))
     426      FAIL ();
     427    if (__builtin_object_size (&x.c[0], 2)
     428        != sizeof (x) - __builtin_offsetof (struct B, c))
     429      FAIL ();
     430    if (__builtin_object_size (&x.c[1], 2)
     431        != sizeof (x) - __builtin_offsetof (struct B, c) - 1)
     432      FAIL ();
     433    if (__builtin_object_size (&x.d, 2)
     434        != sizeof (x) - __builtin_offsetof (struct B, d))
     435      FAIL ();
     436    if (__builtin_object_size (&x.e, 2)
     437        != sizeof (x) - __builtin_offsetof (struct B, e))
     438      FAIL ();
     439    if (__builtin_object_size (&x.f, 2)
     440        != sizeof (x) - __builtin_offsetof (struct B, f))
     441      FAIL ();
     442    dp = &__real__ x.f;
     443    if (__builtin_object_size (dp, 2)
     444        != sizeof (x) - __builtin_offsetof (struct B, f))
     445      FAIL ();
     446    dp = &__imag__ x.f;
     447    if (__builtin_object_size (dp, 2)
     448        != sizeof (x) - __builtin_offsetof (struct B, f)
     449  	 - sizeof (x.f) / 2)
     450      FAIL ();
     451    dp = &y;
     452    if (__builtin_object_size (dp, 2) != sizeof (y))
     453      FAIL ();
     454    if (__builtin_object_size (&z, 2) != sizeof (z))
     455      FAIL ();
     456    dp = &__real__ z;
     457    if (__builtin_object_size (dp, 2) != sizeof (z))
     458      FAIL ();
     459    dp = &__imag__ z;
     460    if (__builtin_object_size (dp, 2) != sizeof (z) / 2)
     461      FAIL ();
     462  }
     463  
     464  struct S { unsigned int a; };
     465  
     466  char *
     467  __attribute__ ((noinline))
     468  test4 (char *x, int y)
     469  {
     470    register int i;
     471    struct A *p;
     472  
     473    for (i = 0; i < y; i++)
     474      {
     475        p = (struct A *) x;
     476        x = (char *) &p[1];
     477        if (__builtin_object_size (p, 2) != 0)
     478  	FAIL ();
     479      }
     480    return x;
     481  }
     482  
     483  void
     484  __attribute__ ((noinline))
     485  test5 (size_t x)
     486  {
     487    char buf[64];
     488    char *p = &buf[8];
     489    size_t i;
     490  
     491    for (i = 0; i < x; ++i)
     492      p = p + 4;
     493  #ifdef __builtin_object_size
     494    if (__builtin_object_size (p, 2) != sizeof (buf) - 8 - 4 * x)
     495  #else
     496    if (__builtin_object_size (p, 2) != 0)
     497  #endif
     498      FAIL ();
     499    memset (p, ' ', sizeof (buf) - 8 - 4 * 4);
     500  }
     501  
     502  void
     503  __attribute__ ((noinline))
     504  test6 (size_t x)
     505  {
     506    struct T { char buf[64]; char buf2[64]; } t;
     507    char *p = &t.buf[8];
     508    size_t i;
     509  
     510    for (i = 0; i < x; ++i)
     511      p = p + 4;
     512  #ifdef __builtin_object_size
     513    if (__builtin_object_size (p, 2) != sizeof (t) - 8 - 4 * x)
     514  #else
     515    if (__builtin_object_size (p, 2) != 0)
     516  #endif
     517      FAIL ();
     518    memset (p, ' ', sizeof (t) - 8 - 4 * 4);
     519  }
     520  
     521  void
     522  __attribute__ ((noinline))
     523  test7 (void)
     524  {
     525    char buf[64];
     526    struct T { char buf[64]; char buf2[64]; } t;
     527    char *p = &buf[64], *q = &t.buf[64];
     528  
     529    if (__builtin_object_size (p + 64, 2) != 0)
     530      FAIL ();
     531    if (__builtin_object_size (q + 63, 2) != sizeof (t) - 64 - 63)
     532      FAIL ();
     533    if (__builtin_object_size (q + 64, 2) != sizeof (t) - 64 - 64)
     534      FAIL ();
     535    if (__builtin_object_size (q + 256, 2) != 0)
     536      FAIL ();
     537  }
     538  
     539  void
     540  __attribute__ ((noinline))
     541  test8 (void)
     542  {
     543    struct T { char buf[10]; char buf2[10]; } t;
     544    char *p = &t.buf2[-4];
     545    char *q = &t.buf2[0];
     546    if (__builtin_object_size (p, 2) != sizeof (t) - 10 + 4)
     547      FAIL ();
     548    if (__builtin_object_size (q, 2) != sizeof (t) - 10)
     549      FAIL ();
     550    /* GCC only handles additions, not subtractions.  */
     551    q = q - 8;
     552    if (__builtin_object_size (q, 2) != 0
     553        && __builtin_object_size (q, 2) != sizeof (t) - 10 + 8)
     554      FAIL ();
     555    p = &t.buf[-4];
     556    if (__builtin_object_size (p, 2) != 0)
     557      FAIL ();
     558  }
     559  
     560  void
     561  __attribute__ ((noinline))
     562  test9 (unsigned cond)
     563  {
     564    char *buf2 = malloc (10);
     565    char *p;
     566  
     567    if (cond)
     568      p = &buf2[8];
     569    else
     570      p = &buf2[4];
     571  
     572  #ifdef __builtin_object_size
     573    if (__builtin_object_size (&p[-4], 2) != (cond ? 6 : 10))
     574      FAIL ();
     575  #else
     576    if (__builtin_object_size (&p[-4], 2) != 0)
     577      FAIL ();
     578  #endif
     579  
     580    for (unsigned i = cond; i > 0; i--)
     581      p--;
     582  
     583  #ifdef __builtin_object_size
     584    if (__builtin_object_size (p, 2) != ((cond ? 2 : 6) + cond))
     585      FAIL ();
     586  #else
     587    if (__builtin_object_size (p, 2) != 0)
     588      FAIL ();
     589  #endif
     590  
     591    p = &y.c[8];
     592    for (unsigned i = cond; i > 0; i--)
     593      p--;
     594  
     595  #ifdef __builtin_object_size
     596    if (__builtin_object_size (p, 2)
     597        != sizeof (y) - __builtin_offsetof (struct A, c) - 8 + cond)
     598      FAIL ();
     599  #else
     600    if (__builtin_object_size (p, 2) != 0)
     601      FAIL ();
     602  #endif
     603  }
     604  
     605  void
     606  __attribute__ ((noinline))
     607  test10 (void)
     608  {
     609    static char buf[255];
     610    unsigned int i, len = sizeof (buf);
     611    char *p = buf;
     612  
     613    for (i = 0 ; i < sizeof (buf) ; i++)
     614      {
     615        if (len < 2)
     616  	{
     617  #ifdef __builtin_object_size
     618  	  if (__builtin_object_size (p - 3, 2) != sizeof (buf) - i + 3)
     619  	    FAIL ();
     620  #else
     621  	  if (__builtin_object_size (p - 3, 2) != 0)
     622  	    FAIL ();
     623  #endif
     624  	  break;
     625  	}
     626        p++;
     627        len--;
     628      }
     629  }
     630  
     631  /* Tests for strdup/strndup.  */
     632  size_t
     633  __attribute__ ((noinline))
     634  test11 (void)
     635  {
     636    const char *ptr = "abcdefghijklmnopqrstuvwxyz";
     637    char *res = strndup (ptr, 21);
     638    if (__builtin_object_size (res, 2) != 22)
     639      FAIL ();
     640  
     641    free (res);
     642  
     643    res = strndup (ptr, 32);
     644    if (__builtin_object_size (res, 2) != 27)
     645      FAIL ();
     646  
     647    free (res);
     648  
     649    res = strdup (ptr);
     650    if (__builtin_object_size (res, 2) != 27)
     651      FAIL ();
     652  
     653    free (res);
     654  
     655    char *ptr2 = malloc (64);
     656    strcpy (ptr2, ptr);
     657  
     658    res = strndup (ptr2, 21);
     659    if (__builtin_object_size (res, 2) != 1)
     660      FAIL ();
     661  
     662    free (res);
     663  
     664    res = strndup (ptr2, 32);
     665    if (__builtin_object_size (res, 2) != 1)
     666      FAIL ();
     667  
     668    free (res);
     669  
     670    res = strndup (ptr2, 128);
     671    if (__builtin_object_size (res, 2) != 1)
     672      FAIL ();
     673  
     674    free (res);
     675  
     676    res = strdup (ptr2);
     677  
     678  #ifdef __builtin_object_size
     679    if (__builtin_object_size (res, 2) != 27)
     680  #else
     681    if (__builtin_object_size (res, 2) != 1)
     682  #endif
     683      FAIL ();
     684  
     685    free (res);
     686    free (ptr2);
     687  
     688    ptr = "abcd\0efghijklmnopqrstuvwxyz";
     689    res = strdup (ptr);
     690    if (__builtin_object_size (res, 2) != 5)
     691      FAIL ();
     692    free (res);
     693  
     694    res = strndup (ptr, 24);
     695    if (__builtin_object_size (res, 2) != 5)
     696      FAIL ();
     697    free (res);
     698  
     699    res = strndup (ptr, 2);
     700    if (__builtin_object_size (res, 2) != 3)
     701      FAIL ();
     702    free (res);
     703  
     704    res = strdup (&ptr[4]);
     705    if (__builtin_object_size (res, 2) != 1)
     706      FAIL ();
     707    free (res);
     708  
     709    res = strndup (&ptr[4], 4);
     710    if (__builtin_object_size (res, 2) != 1)
     711      FAIL ();
     712    free (res);
     713  
     714    res = strndup (&ptr[4], 1);
     715    if (__builtin_object_size (res, 2) != 1)
     716      FAIL ();
     717    free (res);
     718  }
     719  
     720  int
     721  main (void)
     722  {
     723    struct S s[10];
     724    __asm ("" : "=r" (l1) : "0" (l1));
     725    test1 (main, 6);
     726    test2 ();
     727    test3 ();
     728    test4 ((char *) s, 10);
     729    test5 (4);
     730    test6 (4);
     731    test7 ();
     732    test8 ();
     733    test9 (1);
     734    test10 ();
     735    test11 ();
     736    DONE ();
     737  }