1  /* See e.g. https://en.cppreference.com/w/c/io/fprintf
       2     and https://www.man7.org/linux/man-pages/man3/sprintf.3.html */
       3  
       4  extern int
       5  sprintf(char* dst, const char* fmt, ...)
       6    __attribute__((__nothrow__));
       7  
       8  #define NULL ((void *)0)
       9  
      10  int
      11  test_passthrough (char* dst, const char* fmt)
      12  {
      13    /* This assumes that fmt doesn't have any arguments.  */
      14    return sprintf (dst, fmt);
      15  }
      16  
      17  void
      18  test_known (void)
      19  {
      20    char buf[10];
      21    int res = sprintf (buf, "foo");
      22    /* TODO: ideally we would know the value of "res" is 3,
      23       and known the content and strlen of "buf" after the call */
      24  }
      25  
      26  int
      27  test_null_dst (void)
      28  {
      29    return sprintf (NULL, "hello world"); /* { dg-warning "use of NULL where non-null expected" } */
      30  }
      31  
      32  int
      33  test_null_fmt (char *dst)
      34  {
      35    return sprintf (dst, NULL);  /* { dg-warning "use of NULL where non-null expected" } */
      36  }
      37  
      38  int
      39  test_uninit_dst (void)
      40  {
      41    char *dst;
      42    return sprintf (dst, "hello world"); /* { dg-warning "use of uninitialized value 'dst'" } */
      43  }
      44  
      45  int
      46  test_uninit_fmt_ptr (char *dst)
      47  {
      48    const char *fmt;
      49    return sprintf (dst, fmt); /* { dg-warning "use of uninitialized value 'fmt'" } */
      50  }
      51  
      52  int
      53  test_uninit_fmt_buf (char *dst)
      54  {
      55    const char fmt[10];
      56    return sprintf (dst, fmt); // TODO (PR analyzer/105899): complain about "fmt" not being initialized
      57  }
      58  
      59  int
      60  test_fmt_not_terminated (char *dst)
      61  {
      62    const char fmt[3] = "foo";
      63    return sprintf (dst, fmt); // TODO (PR analyzer/105899): complain about "fmt" not being terminated
      64  }