1  /* { dg-do compile } */
       2  /* { dg-options "-O2" } */
       3  
       4  typedef __SIZE_TYPE__ size_t;
       5  
       6  #define bos(__d) __builtin_object_size ((__d), 0)
       7  
       8  char *
       9  safe1 (const char *src, int cond, size_t len)
      10  {
      11    char *dst;
      12  
      13    if (cond)
      14      dst = __builtin_malloc (1024);
      15    else
      16      dst = __builtin_malloc (2048);
      17  
      18    len = len > 2048 ? 2048 : len;
      19  
      20    return __builtin___memcpy_chk (dst, src, len, bos (dst));
      21  }
      22  
      23  char *
      24  safe2 (const char *src, int cond, unsigned char len)
      25  {
      26    char *dst;
      27  
      28    if (cond)
      29      dst = __builtin_malloc (1024);
      30    else
      31      dst = __builtin_malloc (2048);
      32  
      33    return __builtin___strncpy_chk (dst, src, len, bos (dst));
      34  }
      35  
      36  int
      37  safe3 (const char *src, int cond, unsigned char len)
      38  {
      39    char *dst;
      40  
      41    if (cond)
      42      dst = __builtin_malloc (1024);
      43    else
      44      dst = __builtin_malloc (2048);
      45  
      46    return __builtin___snprintf_chk (dst, len, 0, bos (dst), "%s", src);
      47  }
      48  
      49  char dst[1024];
      50  
      51  void
      52  safe4 (size_t len)
      53  {
      54    len = len > sizeof (dst) - 1 ? sizeof (dst) - 1 : len;
      55    len = len < sizeof (dst) / 2 ? sizeof (dst) / 2 : len;
      56  
      57    __builtin_strncat (dst, "hello", len);
      58  }
      59  
      60  /* { dg-final { scan-assembler-not "__memcpy_chk" } } */
      61  /* { dg-final { scan-assembler-not "__strncpy_chk" } } */
      62  /* { dg-final { scan-assembler-not "__snprintf_chk" } } */
      63  /* { dg-final { scan-assembler-not "strncat" } } */