On Mon, Mar 21, 2016 at 4:43 AM, Dmitry V. Levin <l...@altlinux.org> wrote:
> On Sun, Mar 20, 2016 at 11:12:08AM +0530, Jay Joshi wrote:
>> --- a/tests/Makefile.am
>> +++ b/tests/Makefile.am
>> @@ -49,6 +49,7 @@ libtests_a_SOURCES = \
>>       tail_alloc.c \
>>       tests.h \
>>       tprintf.c \
>> +     print_quoted_string.c \
>>       # end of libtests_a_SOURCES
>
> Please keep the list sorted.
>
>> --- /dev/null
>> +++ b/tests/print_quoted_string.c
>> @@ -0,0 +1,61 @@
>> +#include "tests.h"
>> +
>> +#include <stdio.h>
>> +#include <stdlib.h>
>> +
>> +/* Modified from string_quote() from util.c.
>> + * Assumes str is NUL-terminated.
>> + */
>> +
>> +void
>> +print_quoted_string(const char *str)
>> +{
>> +     unsigned int i=0;
>> +     int c;
>
> Shouldn't "c" have type "unsigned char", or, alternatively,
> shouldn't "str" be cast to "const unsigned char *", to avoid sign
> extension?

Indeed.

>
>> +
>> +     while (c = str[i++]) {
>
> Wouldn't it be better without "i" iterator at all?  e.g.
>         while ((c = *(str++))) {
>
>> +                     default:
>> +                             if (c >= ' ' && c <= 0x7e)
>> +                                     putchar(c);
>> +                             else {
>> +                                     printf("\\");
>
> Wouldn't putchar(c) in cases like this look simpler?
>
>> +                                     if (str[i + 1] >= '0' && str[i + 1] <= 
>> '9') {
>
> This check is not correct because "i" already points to the next
> character.
>

I shouldn't have made this mistake. Thanks.
Patch is attached. I've assigned character block of 3-bits to other
characters to avoid text-overflow.
From 7d06f6be125cab56b81ffb606b1b9bcd2dbdc74f Mon Sep 17 00:00:00 2001
From: JayRJoshi <jay.r.joshi...@gmail.com>
Date: Sun, 20 Mar 2016 10:35:15 +0530
Subject: [PATCH] tests: add getcwd.test, add print_quoted_string function to
 libtests

* tests/tests.h (print_quoted_string): New prototype.
* tests/print_quoted_string.c: New file.
* tests/Makefile.am (libtests_a_SOURCES): Add it.
* tests/getcwd.c: New file.
* tests/getcwd.test: New test.
* tests/.gitignore: Add getcwd.
* tests/Makefile.am (check_PROGRAMS): Likewise.
(TESTS): Add getcwd.test.
---
 tests/.gitignore            |  1 +
 tests/Makefile.am           |  3 ++
 tests/getcwd.c              | 39 ++++++++++++++++++++++++++
 tests/getcwd.test           | 13 +++++++++
 tests/print_quoted_string.c | 67 +++++++++++++++++++++++++++++++++++++++++++++
 tests/tests.h               |  3 ++
 6 files changed, 126 insertions(+)
 create mode 100644 tests/getcwd.c
 create mode 100755 tests/getcwd.test
 create mode 100644 tests/print_quoted_string.c

diff --git a/tests/.gitignore b/tests/.gitignore
index 449af18..d1fb6d4 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -42,6 +42,7 @@ fstat64
 fstatat64
 ftruncate
 ftruncate64
+getcwd
 getdents
 getdents64
 getrandom
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3005382..60b1087 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -46,6 +46,7 @@ libtests_a_SOURCES = \
 	hexdump_strdup.c \
 	hexquote_strndup.c \
 	inode_of_sockfd.c \
+	print_quoted_string.c \
 	tail_alloc.c \
 	tests.h \
 	tprintf.c \
@@ -92,6 +93,7 @@ check_PROGRAMS = \
 	fstatat64 \
 	ftruncate \
 	ftruncate64 \
+	getcwd \
 	getdents \
 	getdents64 \
 	getrandom \
@@ -268,6 +270,7 @@ TESTS = \
 	fstatat64.test \
 	ftruncate.test \
 	ftruncate64.test \
+	getcwd.test \
 	getdents.test \
 	getdents64.test \
 	getrandom.test \
diff --git a/tests/getcwd.c b/tests/getcwd.c
new file mode 100644
index 0000000..3135df2
--- /dev/null
+++ b/tests/getcwd.c
@@ -0,0 +1,39 @@
+#include "tests.h"
+
+#include <sys/syscall.h>
+
+#ifdef __NR_getcwd
+
+# include <stdio.h>
+# include <unistd.h>
+# include <sys/param.h>
+
+int
+main(void)
+{
+	long res;
+	char cur_dir[PATH_MAX + 1];
+
+	res = syscall(__NR_getcwd, cur_dir, sizeof(cur_dir));
+
+	if (res <= 0)
+		perror_msg_and_fail("getcwd");
+
+	printf("getcwd(\"");
+	print_quoted_string(cur_dir);
+	printf("\", %zu) = %ld\n", sizeof(cur_dir), res);
+
+	syscall(__NR_getcwd, cur_dir, 0);
+
+	printf("getcwd(%p, 0) = -1 ERANGE (%m)\n", cur_dir);
+
+	puts("+++ exited with 0 +++");
+
+	return 0;
+}
+
+#else
+
+SKIP_MAIN_UNDEFINED("__NR_getcwd");
+
+#endif
diff --git a/tests/getcwd.test b/tests/getcwd.test
new file mode 100755
index 0000000..babb0de
--- /dev/null
+++ b/tests/getcwd.test
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+# Check getcwd syscall decoding.
+
+. "${srcdir=.}/init.sh"
+
+run_prog > /dev/null
+OUT="$LOG.out"
+run_strace -egetcwd -a18 $args > $OUT
+match_diff "$LOG" "$OUT"
+rm -f "$OUT"
+
+exit 0
diff --git a/tests/print_quoted_string.c b/tests/print_quoted_string.c
new file mode 100644
index 0000000..8ad6a25
--- /dev/null
+++ b/tests/print_quoted_string.c
@@ -0,0 +1,67 @@
+#include "tests.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+/* Modified from string_quote() from util.c.
+ * Assumes instr is NUL-terminated.
+ */
+
+void
+print_quoted_string(const char *instr)
+{
+	const unsigned char *str = (const unsigned char*) instr;
+	int c;
+
+	while ((c = *(str++))) {
+		switch (c) {
+			case '\"':
+				printf("\\\"");
+				break;
+			case '\\':
+				printf("\\\\");
+				break;
+			case '\f':
+				printf("\\f");
+				break;
+			case '\n':
+				printf("\\n");
+				break;
+			case '\r':
+				printf("\\r");
+				break;
+			case '\t':
+				printf("\\t");
+				break;
+			case '\v':
+				printf("\\v");
+				break;
+			default:
+				if (c >= ' ' && c <= 0x7e)
+					putchar(c);
+				else {
+					putchar('\\');
+
+					char c1 = '0' + (c & 0x7);
+					char c2 = '0' + ((c >> 3) & 0x7);
+					char c3 = '0' + (c >> 6);
+
+					if (*str >= '0' && *str <= '9') {
+						/* Print \octal */
+						putchar(c3);
+						putchar(c2);
+					} else {
+						/* Print \[[o]o]o */
+						if ((c >> 3) != 0) {
+							if ((c >> 6) != 0)
+								putchar(c3);
+							putchar(c2);
+						}
+					}
+					putchar(c1);
+				}
+				break;
+		}
+	}
+
+}
diff --git a/tests/tests.h b/tests/tests.h
index 826f8b2..870ea5d 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -75,6 +75,9 @@ const char *hexquote_strndup(const char *, size_t);
 /* Return inode number of socket descriptor. */
 unsigned long inode_of_sockfd(int);
 
+/* Print string in escaped format. */
+void print_quoted_string(const char *str);
+
 # define ARRAY_SIZE(arg) ((unsigned int) (sizeof(arg) / sizeof((arg)[0])))
 # define LENGTH_OF(arg) ((unsigned int) sizeof(arg) - 1)
 
-- 
1.9.1

------------------------------------------------------------------------------
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785351&iu=/4140
_______________________________________________
Strace-devel mailing list
Strace-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/strace-devel

Reply via email to