[LTP] [PATCH v2] getcwd01: Use syscall directly check invalid argument
Wei Gao
wegao@suse.com
Fri Sep 29 02:49:14 CEST 2023
On Thu, Sep 28, 2023 at 07:55:29PM +0200, Petr Vorel wrote:
> Hi Wei,
>
> Yes, syscall(__NR_getcwd, ...) is way much better than #ifdef __GLIBC__ in v2.
>
> > Related issue: https://github.com/linux-test-project/ltp/issues/1084
> This should be Fixes: #1084
>
>
> > Signed-off-by: Wei Gao <wegao@suse.com>
> > ---
> > testcases/kernel/syscalls/getcwd/getcwd01.c | 23 +++++++++++----------
> > 1 file changed, 12 insertions(+), 11 deletions(-)
>
> > diff --git a/testcases/kernel/syscalls/getcwd/getcwd01.c b/testcases/kernel/syscalls/getcwd/getcwd01.c
> > index 65d827873..d47174ffc 100644
> > --- a/testcases/kernel/syscalls/getcwd/getcwd01.c
> > +++ b/testcases/kernel/syscalls/getcwd/getcwd01.c
> > @@ -14,8 +14,8 @@
> > *
> > * Expected Result:
> > * 1) getcwd(2) should return NULL and set errno to EFAULT.
> > - * 2) getcwd(2) should return NULL and set errno to ENOMEM.
> > - * 3) getcwd(2) should return NULL and set errno to EINVAL.
> > + * 2) getcwd(2) should return NULL and set errno to EFAULT.
> > + * 3) getcwd(2) should return NULL and set errno to ERANGE.
> > * 4) getcwd(2) should return NULL and set errno to ERANGE.
> > * 5) getcwd(2) should return NULL and set errno to ERANGE.
> > *
> > @@ -24,6 +24,7 @@
> > #include <errno.h>
> > #include <unistd.h>
> > #include <limits.h>
> > +#include "lapi/syscalls.h"
> > #include "tst_test.h"
>
> > static char buffer[5];
> > @@ -34,32 +35,32 @@ static struct t_case {
> > int exp_err;
> > } tcases[] = {
> > {(void *)-1, PATH_MAX, EFAULT},
> > - {NULL, (size_t)-1, ENOMEM},
> > - {buffer, 0, EINVAL},
> > + {NULL, (size_t)-1, EFAULT},
> > + {buffer, 0, ERANGE},
> > {buffer, 1, ERANGE},
> > {NULL, 1, ERANGE}
> > };
>
> > +
> > static void verify_getcwd(unsigned int n)
> > {
> > struct t_case *tc = &tcases[n];
> > - char *res;
> > + int res;
>
> > errno = 0;
> > - res = getcwd(tc->buf, tc->size);
> > - TST_ERR = errno;
> > - if (res) {
> > + res = syscall(__NR_getcwd, tc->buf, tc->size);
> > + if (!res) {
> > tst_res(TFAIL, "getcwd() succeeded unexpectedly");
> > return;
> > }
>
> > - if (TST_ERR != tc->exp_err) {
> > - tst_res(TFAIL | TTERRNO, "getcwd() failed unexpectedly, expected %s",
> > + if (errno != tc->exp_err) {
> > + tst_res(TFAIL, "getcwd() failed unexpectedly, expected %s",
> Hm, macros in tst_test_macros.h does not support char * (getcwd() returns NULL
> on failure. I wonder if we want to add TST_EXP_FAIL3(), or maybe macro with a
> better name (TST_EXP_FAIL_STR()).
This case we can use TST_EXP_FAIL2 since syscall for __NR_getcwd will return the length of the buffer filled
instead of char *.
Yes, we are missing macros to support char *, i think i can create one in another patch.
>
> Kind regards,
> Petr
>
> > tst_strerrno(tc->exp_err));
> > return;
> > }
>
> > - tst_res(TPASS | TTERRNO, "getcwd() failed as expected");
> > + tst_res(TPASS, "getcwd() failed as expected");
> > }
More information about the ltp
mailing list