[PATCH v1] tests: getcwd.test

Jay Joshi jay.r.joshi100 at gmail.com
Sat Mar 19 19:49:16 UTC 2016


On Sat, Mar 19, 2016 at 10:06 PM, Dmitry V. Levin <ldv at altlinux.org> wrote:
> On Sat, Mar 19, 2016 at 06:34:34PM +0530, Jay Joshi wrote:
>> On Sat, Mar 19, 2016 at 5:07 AM, Dmitry V. Levin <ldv at altlinux.org> wrote:
>> > On Fri, Mar 18, 2016 at 10:14:26AM +0530, Jay Joshi wrote:
>> >> >From 6785e2dc6f15104537d2e27387279a04c9bcfb25 Mon Sep 17 00:00:00 2001
>> >> From: JayRJoshi <jay.r.joshi100 at gmail.com>
>> >> Date: Tue, 15 Mar 2016 16:41:39 +0530
>> >> Subject: [PATCH] tests: add getcwd.test
>> +int
>> +main(void)
>> +{
>> +  long res;
>> +  char cur_dir[PATH_MAX + 1];
>> +
>> +  res = syscall(__NR_getcwd, cur_dir, sizeof(cur_dir));
>> +
>> +  if (res != 0) {
>
> Why do you check for != 0?

It should be res<=0. Just a safety check against bugs, like system
might allow dir path bigger than PATH_MAX.

>> diff --git a/tests/getcwd.test b/tests/getcwd.test
>> new file mode 100755
>> index 0000000..c744c0e
>> --- /dev/null
>> +++ b/tests/getcwd.test
>> @@ -0,0 +1,11 @@
>> +#!/bin/sh
>
> Please add a short comment what's being tested here.
>
>> --- /dev/null
>> +++ b/tests/print_escaped_string.c
>
> Please rename print_escaped_string -> print_quoted_string
>
>> @@ -0,0 +1,80 @@
>> +#include "tests.h"
>> +
>> +#include <stdio.h>
>> +#include <stdlib.h>
>> +
>> +/* Modified from string_quote() from util.c
>> + * Assumes str is NUL-terminated.
>> + */
>> +
>> +void
>> +print_escaped_string(const char *str)
>> +{
>> + const unsigned char *ustr = (const unsigned char *) str;
>> + size_t size = sizeof(str);
>> +  size_t esc_size = 4 * size;
>> +  char *esc_str, *s;
>> + s = esc_str = (char *) malloc((char) esc_size);
>> +
>> +  if(s == NULL)
>> +    perror_msg_and_fail("malloc(%zu)", esc_size);
>> +
>> + unsigned int i=0;
>> + int c;
>> +
>> + while ((c = ustr[i++]) != '\0') {
>> + switch (c) {
>> + case '\"': case '\\':
>> + *s++ = '\\';
>> + *s++ = c;
>> + break;
>> + case '\f':
>> + *s++ = '\\';
>> + *s++ = 'f';
>> + break;
>> + case '\n':
>> + *s++ = '\\';
>> + *s++ = 'n';
>> + break;
>> + case '\r':
>> + *s++ = '\\';
>> + *s++ = 'r';
>> + break;
>> + case '\t':
>> + *s++ = '\\';
>> + *s++ = 't';
>> + break;
>> + case '\v':
>> + *s++ = '\\';
>> + *s++ = 'v';
>> + break;
>> + default:
>> + if (c >= ' ' && c <= 0x7e)
>> + *s++ = c;
>> + else {
>> + /* if str contains characters from ' ' to '~', than
>> +           * else won't be required,
>> +           * also esc_size would be 2 * size - 1.
>> +           */
>> + *s++ = '\\';
>> + if (i + 1 < size
>> +    && ustr[i + 1] >= '0'
>> +    && ustr[i + 1] <= '9'
>> + ) {
>> + *s++ = '0' + (c >> 6);
>> + *s++ = '0' + ((c >> 3) & 0x7);
>> + } else {
>> + if ((c >> 3) != 0) {
>> + if ((c >> 6) != 0)
>> + *s++ = '0' + (c >> 6);
>> + *s++ = '0' + ((c >> 3) & 0x7);
>> + }
>> + }
>> + *s++ = '0' + (c & 0x7);
>> + }
>> + break;
>> + }
>> +  }
>> +  *s = '\0';
>> +  printf("%s",esc_str);
>> +}
>
> Unlike string_quote(), this is not indented properly and therefore
> hard to read.

I'm using TAB for indentation. Pasting it here is creating formatting
problem. That's why I'm attaching the patch.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: a.patch
Type: text/x-patch
Size: 4871 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20160320/bd91c715/attachment.bin>


More information about the Strace-devel mailing list