(root)/
strace-6.5/
tests/
printstr.c
       1  /*
       2   * Check decoding of non-NUL-terminated strings when len == -1.
       3   *
       4   * Copyright (c) 2016 Dmitry V. Levin <ldv@strace.io>
       5   * Copyright (c) 2016-2021 The strace developers.
       6   * All rights reserved.
       7   *
       8   * SPDX-License-Identifier: GPL-2.0-or-later
       9   */
      10  
      11  #include "tests.h"
      12  
      13  #include <stdio.h>
      14  #include <string.h>
      15  #include <unistd.h>
      16  #include <sys/uio.h>
      17  
      18  int
      19  main(void)
      20  {
      21  	char *const buf = tail_alloc(DEFAULT_STRLEN + 1);
      22  	struct iovec io = {
      23  		.iov_base = buf,
      24  		.iov_len = -1
      25  	};
      26  	int rc;
      27  
      28  	buf[0] = 0;
      29  
      30  	tprintf("%s", "");
      31  
      32  	memset(buf + 1, 'X', DEFAULT_STRLEN);
      33  	buf[DEFAULT_STRLEN - 1] = 0;
      34  
      35  	rc = writev(-1, &io, 1);
      36  	tprintf("writev(-1, [{iov_base=\"\\0%*s\\0\"..., iov_len=%lu}], 1)"
      37  		" = %s\n", DEFAULT_STRLEN - 2, buf + 1, -1UL, sprintrc(rc));
      38  
      39  	buf[DEFAULT_STRLEN - 1] = 'X';
      40  	buf[DEFAULT_STRLEN] = 0;
      41  
      42  	rc = writev(-1, &io, 1);
      43  	tprintf("writev(-1, [{iov_base=\"\\0%*s\"..., iov_len=%lu}], 1)"
      44  		" = %s\n", DEFAULT_STRLEN - 1, buf + 1, -1UL, sprintrc(rc));
      45  
      46  	++io.iov_base;
      47  	rc = writev(-1, &io, 1);
      48  	tprintf("writev(-1, [{iov_base=%p, iov_len=%lu}], 1) = %s\n",
      49  		io.iov_base, -1UL, sprintrc(rc));
      50  
      51  	tprintf("+++ exited with 0 +++\n");
      52  	return 0;
      53  }