Re: Big endian and strict alignment

2021-01-19 Thread Masato Asou
From: Masato Asou 
Date: Tue, 19 Jan 2021 17:02:42 +0900 (JST)

> お客さんから「何か情報知ってますか?」という問い合わせがあったので、ど
> なたか情報をお持ちの方がいたら、情報提供をお願いします。


Sorry.
I made a mistake in the address of the email.
--
ASOU Masato



Big endian and strict alignment

2021-01-19 Thread Masato Asou
お客さんから「何か情報知ってますか?」という問い合わせがあったので、ど
なたか情報をお持ちの方がいたら、情報提供をお願いします。

要望としては、以下の二点 (いずれか一方でも可) を満たす Raspberry Pi の
ように手軽に入手できるマシンありませんか?というものです。

  - Big endian
  - Strict alignment: MIPS のように WORD (4byte) の読み書きは 4byte 境
界でなければならない。
--
ASOU Masato



Re: [PATCH] Fix a bug where GDB could not display symbols

2020-11-30 Thread Masato Asou
I fixed the patch a bit.

The bt command does not work correctly with the core created when
calling the ABORT(3) as follows:

$ cat main.c
#include 
#include 
#include 

int
main(int argc, char *argv[])
{
printf("argc = %d\n", argc);
abort();
return (0);
}
$ cc -static main.c
$ ./a.out
argc = 1
Abort trap (core dumped)
$ /usr/bin/gdb a.out a.out.core
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for
details.
This GDB was configured as "amd64-unknown-openbsd6.8"...
Core was generated by `a.out'.
Program terminated with signal 6, Aborted.
#0  0x05c6e0400c1a in ?? ()
(gdb) bt
#0  0x05c6e0400c1a in ?? ()
#1  0x05c6e0400bbe in ?? ()
#2  0x00300010 in ?? ()
#3  0x7f7beec0 in ?? ()
#4  0xffdbede0 in ?? ()
#5  0x7f7bef48 in ?? ()
#6  0x7f7beee0 in ?? ()
#7  0x05c6e03ecf5f in ?? ()
#8  0x0007 in ?? ()
#9  0x0009fbe83b4e in ?? ()
#10 0x7f7bef48 in ?? ()
#11 0x0001 in ?? ()
#12 0x7f7bef30 in ?? ()
#13 0x05c6e03ecd23 in ?? ()
#14 0x05c6e0406078 in ?? ()
#15 0x7f7bef58 in ?? ()
#16 0x0001 in ?? ()
#17 0x in ?? ()
(gdb) quit

I applied the new patch to /usr/obj/gnu/usr.bin/binutils/gdb/gdb.

$ /usr/obj/gnu/usr.bin/binutils/gdb/gdb a.out a.out.core
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for
details.
This GDB was configured as "amd64-unknown-openbsd6.8"...
Core was generated by `a.out'.
Program terminated with signal 6, Aborted.
#0  thrkill () at /tmp/-:3
3   /tmp/-: No such file or directory.
in /tmp/-
(gdb) bt
#0  thrkill () at /tmp/-:3
#1  0x05c6e0400bbe in _libc_abort () at
/usr/src/lib/libc/stdlib/abort.c:51
#2  0x05c6e03ecf5f in main ()
Current language:  auto; currently asm
(gdb) quit
$ 

I added exec_set_section_offsets() after do_cleanups() in the previous
patch.

ok? comment?

Index: solib-svr4.c
===
RCS file: /cvs/src/gnu/usr.bin/binutils/gdb/solib-svr4.c,v
retrieving revision 1.2
diff -u -p -r1.2 solib-svr4.c
--- solib-svr4.c11 Nov 2008 22:57:48 -  1.2
+++ solib-svr4.c30 Nov 2020 23:01:42 -
@@ -619,7 +619,41 @@ svr4_current_sos (void)
   /* If we can't find the dynamic linker's base structure, this
 must not be a dynamically linked executable.  Hmm.  */
   if (! debug_base)
-   return 0;
+   {
+ if (exec_bfd != NULL &&
+ bfd_get_section_by_name (exec_bfd, ".interp") == NULL &&
+ (bfd_get_file_flags (exec_bfd) & DYNAMIC) != 0 &&
+ bfd_get_start_address (exec_bfd) != entry_point_address ())
+   {
+ /* this is relocatable static link.
+cf. svr4_relocate_main_executable() */
+ struct cleanup *old_chain;
+ struct section_offsets *new_offsets;
+ int i, changed;
+ CORE_ADDR displacement;
+
+ displacement = entry_point_address () - bfd_get_start_address 
(exec_bfd);
+ changed = 0;
+
+ new_offsets = xcalloc (symfile_objfile->num_sections,
+sizeof (struct section_offsets));
+ old_chain = make_cleanup (xfree, new_offsets);
+
+ for (i = 0; i < symfile_objfile->num_sections; i++)
+   {
+ if (displacement != ANOFFSET 
(symfile_objfile->section_offsets, i))
+   changed = 1;
+ new_offsets->offsets[i] = displacement;
+   }
+
+ if (changed)
+   objfile_relocate (symfile_objfile, new_offsets);
+
+ do_cleanups (old_chain);
+ exec_set_section_offsets(displacement, displacement, 
displacement);
+   }
+     return 0;
+   }
 }
 
   /* Walk the inferior's link map list, and build our list of
--
ASOU Masato


From: Masato Asou 
Date: Thu, 08 Oct 2020 14:07:15 +0900 (JST)

> I refferd to the core of static linked in GDB.  However, the backtrace
> command did not display the symbols correctly.
> 
> $ cat main.c
> #include 
> 
> void
> sub2(int argc, char *argv[])
> {
> int i;
> for (int i = 0; i <= argc; i++)
> argv[i][0] = '\0';
> }
>

[PATCH] Fix a bug where GDB could not display symbols

2020-10-07 Thread Masato Asou
I refferd to the core of static linked in GDB.  However, the backtrace
command did not display the symbols correctly.

$ cat main.c
#include 

void
sub2(int argc, char *argv[])
{
int i;
for (int i = 0; i <= argc; i++)
argv[i][0] = '\0';
}

void
sub1(int argc, char *argv[])
{
sub2(argc, argv);
}

int
main(int argc, char *argv[])
{
sub1(argc, argv);
return (0);
}
$ cc -g -static main.c
$ ./a.out
Segmentation fault (core dumped)
$ /usr/bin/gdb a.out a.out.core
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "amd64-unknown-openbsd6.8"...
Core was generated by `a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x060eed558f45 in ?? ()
(gdb) bt
#0  0x060eed558f45 in ?? ()
#1  0x7f7e3480 in ?? ()
#2  0x060eed558f98 in ?? ()
#3  0x in ?? ()
(gdb) quit
$ 

The patch is below.
ok?

diff --git a/gnu/usr.bin/binutils/gdb/solib-svr4.c 
b/gnu/usr.bin/binutils/gdb/solib-svr4.c
index eebeddd..7428bda 100644
--- a/gnu/usr.bin/binutils/gdb/solib-svr4.c
+++ b/gnu/usr.bin/binutils/gdb/solib-svr4.c
@@ -619,7 +619,40 @@ svr4_current_sos (void)
   /* If we can't find the dynamic linker's base structure, this
 must not be a dynamically linked executable.  Hmm.  */
   if (! debug_base)
-   return 0;
+   {
+ if (exec_bfd != NULL &&
+ bfd_get_section_by_name (exec_bfd, ".interp") == NULL &&
+ (bfd_get_file_flags (exec_bfd) & DYNAMIC) != 0 &&
+ bfd_get_start_address (exec_bfd) != entry_point_address ())
+   {
+ /* this is relocatable static link.
+cf. svr4_relocate_main_executable() */
+ struct cleanup *old_chain;
+ struct section_offsets *new_offsets;
+ int i, changed;
+ CORE_ADDR displacement;
+
+ displacement = entry_point_address () - bfd_get_start_address 
(exec_bfd);
+ changed = 0;
+
+ new_offsets = xcalloc (symfile_objfile->num_sections,
+sizeof (struct section_offsets));
+ old_chain = make_cleanup (xfree, new_offsets);
+
+ for (i = 0; i < symfile_objfile->num_sections; i++)
+   {
+ if (displacement != ANOFFSET 
(symfile_objfile->section_offsets, i))
+   changed = 1;
+ new_offsets->offsets[i] = displacement;
+   }
+
+ if (changed)
+   objfile_relocate (symfile_objfile, new_offsets);
+
+ do_cleanups (old_chain);
+   }
+ return 0;
+   }
 }
 
   /* Walk the inferior's link map list, and build our list of
--
ASOU Masato



Re: Is uvm_map_isavail() returning 1 instead of -1?

2019-12-13 Thread Masato Asou
From: "Theo de Raadt" 
Subject: Re: Is uvm_map_isavail() returning 1 instead of -1?
Date: Fri, 13 Dec 2019 15:54:10 -0700

> Masato Asou  wrote:
> 
>> Is this correct?
>> 
>> Index: sys/uvm/uvm_map.c
>> ===
>> RCS file: /cvs/src/sys/uvm/uvm_map.c,v
>> retrieving revision 1.259
>> diff -u -p -r1.259 uvm_map.c
>> --- sys/uvm/uvm_map.c   12 Dec 2019 11:12:36 -  1.259
>> +++ sys/uvm/uvm_map.c   13 Dec 2019 22:45:40 -
>> @@ -855,7 +855,7 @@ uvm_map_isavail(struct vm_map *map, stru
>> }
>> }
>>  
>> -   return -1;
>> +   return 1;
>>  }
>>  
>>  /*
> 
> I see only 0 vs non-0 tests for the return value of uvm_map_isavail().
> 
> Am I missing something?

No problem.

Thank you.
--
ASOU Masato



Is uvm_map_isavail() returning 1 instead of -1?

2019-12-13 Thread Masato Asou
Is this correct?

Index: sys/uvm/uvm_map.c
===
RCS file: /cvs/src/sys/uvm/uvm_map.c,v
retrieving revision 1.259
diff -u -p -r1.259 uvm_map.c
--- sys/uvm/uvm_map.c   12 Dec 2019 11:12:36 -  1.259
+++ sys/uvm/uvm_map.c   13 Dec 2019 22:45:40 -
@@ -855,7 +855,7 @@ uvm_map_isavail(struct vm_map *map, stru
}
}
 
-   return -1;
+   return 1;
 }
 
 /*

--
ASOU Masato



thrsleep_unlock: check pointer against NULL

2019-12-04 Thread Masato Asou
ok?

Index: sys/kern/kern_synch.c
===
RCS file: /cvs/src/sys/kern/kern_synch.c,v
retrieving revision 1.155
diff -u -p -r1.155 kern_synch.c
--- sys/kern/kern_synch.c   30 Nov 2019 11:19:17 -  1.155
+++ sys/kern/kern_synch.c   4 Dec 2019 08:42:01 -
@@ -603,7 +603,7 @@ thrsleep_unlock(void *lock)
static _atomic_lock_t unlocked = _ATOMIC_LOCK_UNLOCKED;
_atomic_lock_t *atomiclock = lock;
 
-   if (!lock)
+   if (lock == NULL)
return 0;
 
return copyout(, atomiclock, sizeof(unlocked));
--
ASOU Masato



fwide() does not unlock if error was occurred

2019-12-02 Thread Masato Asou
fwide() does not unlock if error was occurred.

ok?

Index: lib/libc/stdio/fwide.c
===
RCS file: /cvs/src/lib/libc/stdio/fwide.c,v
retrieving revision 1.5
diff -u -p -r1.5 fwide.c
--- lib/libc/stdio/fwide.c  31 Aug 2015 02:53:57 -  1.5
+++ lib/libc/stdio/fwide.c  3 Dec 2019 03:25:26 -
@@ -51,8 +51,10 @@ fwide(FILE *fp, int mode)
 
FLOCKFILE(fp);
wcio = WCIO_GET(fp);
-   if (!wcio)
+   if (!wcio) {
+   FUNLOCKFILE(fp);
return 0; /* XXX */
+   }
 
if (wcio->wcio_mode == 0 && mode != 0)
wcio->wcio_mode = mode;
--
ASOU Masato



Re: OpenBSD 6.x and wxallowed

2019-10-20 Thread Masato Asou
From: "Nelson H. F. Beebe" 
Date: Fri, 18 Oct 2019 07:39:26 -0600

> Has anyone looked into the problem of enumerating packages that are
> installed in the /usr/local tree that actually NEED simultaneous write
> and execute access?

You can find that as below:

$ grep -rHI USE_WXNEEDED /usr/ports/ | wc
  64 2013827
$ 

Above result is my amd64 box with 6.6 current.
--
ASOU Masato



Question about man 2 fcntl

2019-09-04 Thread Masato Asou
The following statement is a part of man 2 fctl.

 [EINTR]The argument cmd is invalid.

The argument cmd is F_SETLKW, and the function
was
interrupted by a signal.

Will EINVAL be returned in the case of 'The argument cmd is invalid'?

Will EINTR be returned for F_SETLK?
The following code is sys/kern/kern_descrip.c sys_fcntl():

case F_SETLKW:
flg |= F_WAIT;
/* FALLTHROUGH */

case F_SETLK:

How about it?

Index: fcntl.2
===
RCS file: /cvs/src/lib/libc/sys/fcntl.2,v
retrieving revision 1.32
diff -u -p -r1.32 fcntl.2
--- fcntl.2 10 Nov 2018 11:54:03 -  1.32
+++ fcntl.2 4 Sep 2019 07:15:33 -
@@ -465,14 +465,14 @@ and a deadlock condition was detected.
 .It Bq Er EINTR
 The argument
 .Fa cmd
-is invalid.
-.Pp
-The argument
-.Fa cmd
 is
-.Dv F_SETLKW ,
+.Dv F_SETLK or F_SETLKW ,
 and the function was interrupted by a signal.
 .It Bq Er EINVAL
+The argument
+.Fa cmd
+is invalid.
+.Pp
 .Fa cmd
 is
 .Dv F_DUPFD
--
ASOU Masato



libedit: Does not run input command in vi mode

2019-09-02 Thread Masato Asou
Does not run input command by vi editor with vi mode.

I do the following:

1. set vi mode.
   $ echo "bind -v" > ~/.editrc

2. launch /usr/bin/ftp command.
   $ ftp

3. launch vi editor with ESC + v.
   ftp> ESC + v

4. input "help" in vi editor.
   i + help + ESC + :wq

5. then 'help' command does not run.

I fix this problem with following patch. This fix is come from NetBSD
lib/libedit/vi.c 1.46 and 1.47.

ok?

Index: vi.c
===
RCS file: /cvs/src/lib/libedit/vi.c,v
retrieving revision 1.27
diff -u -p -r1.27 vi.c
--- vi.c3 Sep 2019 02:28:25 -   1.27
+++ vi.c3 Sep 2019 05:34:31 -
@@ -1058,12 +1058,12 @@ vi_histedit(EditLine *el, wint_t c __att
while (waitpid(pid, , 0) != pid)
continue;
lseek(fd, (off_t)0, SEEK_SET);
-   st = read(fd, cp, TMP_BUFSIZ);
+   st = read(fd, cp, TMP_BUFSIZ - 1);
if (st > 0) {
-   len = (size_t)(el->el_line.lastchar -
-   el->el_line.buffer);
+   cp[st] = '\0';
+   len = (size_t)(el->el_line.limit - el->el_line.buffer);
len = mbstowcs(el->el_line.buffer, cp, len);
-   if (len > 0 && el->el_line.buffer[len -1] == '\n')
+   if (len > 0 && el->el_line.buffer[len - 1] == '\n')
--len;
}
else

--
ASOU Masato



Re: /bin/cp: Incorrect checking the return value

2019-08-18 Thread Masato Asou
From: "Theo de Raadt" 
Date: Fri, 16 Aug 2019 10:01:25 -0600

> Masato Asou  wrote:
> 
>> Incorrect checking the return value of malloc and system calls in
>> /bin/cp.
> 
> The NULL vs ! checks, I cannot agree with those.  Their effect is
> identical and it is commonplace to use either idiom.

I accept your oppinion.

> As to precise-checks for system call return values of -1 or 0, vs
> <, for that case I do not know how to proceed.
> 
> I actually have a diff lying around which does precise -1 checks for the
> entire tree, in regards to section-2 system calls and some of the
> thinner section-3 wrappers.
> 
> I wanted to know if there were any truly incorrect checks, and the
> easiest way was to carefully adjust all the code, and then read the
> diffs afterwards.  I found only two bugs, which I fixed a couple months
> ago.  So this is the remainder.
> 
> In the following diff, I believe the <, <= and != 0 being converted
> to precise == 0 and == -1 are more precise but without error, but I'm
> not sure whether to proceed.  On some architectures, binaries will
> grow minutely larger because a precise check uses a few more instruction
> bytes than a relative range check.
> 
> BTW, there IS some opportunity for making mistakes with such a diff,
> especially around the 64-bit system calls and thin wrappers.
> 
> So I don't think you should proceed with precise system call checks for
> this one program, until we all discuss whether this idiom is valuable to
> do for ALL programs.  Is precise -1 checking a more correct idiom, or is
> it pointless?

I agree that we need to discuss about this idiom

Thank your.
--
ASOU Masato

> (As I said, I'm ok with deleting my 1750 line diff since (1) I have
> started thinking it is pointless, and (2) it was purely the side effort
> of an audit procedure)
> 
> Index: usr.bin/biff/biff.c
> ===
> RCS file: /cvs/src/usr.bin/biff/biff.c,v
> retrieving revision 1.17
> diff -u -p -u -r1.17 biff.c
> --- usr.bin/biff/biff.c   28 Jun 2019 13:35:00 -  1.17
> +++ usr.bin/biff/biff.c   4 Jul 2019 17:00:26 -
> @@ -66,7 +66,7 @@ main(int argc, char *argv[])
>   if (pledge("stdio rpath fattr", NULL) == -1)
>   err(2, "pledge");
>  
> - if (stat(name, ))
> + if (stat(name, ) == -1)
>   err(2, "stat");
>  
>   sb.st_mode &= ACCESSPERMS;
> Index: usr.bin/calendar/calendar.c
> ===
> RCS file: /cvs/src/usr.bin/calendar/calendar.c,v
> retrieving revision 1.37
> diff -u -p -u -r1.37 calendar.c
> --- usr.bin/calendar/calendar.c   1 Feb 2019 16:22:53 -   1.37
> +++ usr.bin/calendar/calendar.c   6 Jul 2019 15:40:49 -
> @@ -167,21 +167,21 @@ main(int argc, char *argv[])
>* we can chdir() we can stat(), unless the user is
>* modifying permissions while this is running.
>*/
> - if (chdir(pw->pw_dir)) {
> + if (chdir(pw->pw_dir) == -1) {
>   if (errno == EACCES)
>   acstat = 1;
>   else
>   continue;
>   }
> - if (stat(calendarFile, ) != 0) {
> - if (chdir(calendarHome)) {
> + if (stat(calendarFile, ) == -1) {
> + if (chdir(calendarHome) == -1) {
>   if (errno == EACCES)
>   acstat = 1;
>   else
>   continue;
>   }
>   if (stat(calendarNoMail, ) == 0 ||
> - stat(calendarFile, ) != 0)
> + stat(calendarFile, ) == -1)
>   continue;
>   }
>   sleeptime = USERTIMEOUT;
> @@ -197,11 +197,11 @@ main(int argc, char *argv[])
>   err(1, "unable to set user context (uid 
> %u)",
>   pw->pw_uid);
>   if (acstat) {
> - if (chdir(pw->pw_dir) ||
> - stat(calendarFile, ) != 0 ||
> - chdir(calendarHome) ||
&g

Re: /bin/cp: Incorrect checking the return value

2019-08-16 Thread Masato Asou
From: Otto Moerbeek 
Date: Fri, 16 Aug 2019 13:56:37 +0200

> On Fri, Aug 16, 2019 at 05:44:35PM +0900, Masato Asou wrote:
> 
>> Incorrect checking the return value of malloc and system calls in
>> /bin/cp.
>> 
>> ok?
> 
> Altough I indeed prefer to check against NULL these are not incorrect,
> just bad idiom.

As you say, the "!" check is not a incorrect.

Thank you.
--
ASOU Masato

>   -Otto
> 
>> 
>> Index: utils.c
>> ===
>> RCS file: /cvs/src/bin/cp/utils.c,v
>> retrieving revision 1.48
>> diff -u -p -U10 -r1.48 utils.c
>> --- utils.c  28 Jun 2019 13:34:58 -  1.48
>> +++ utils.c  16 Aug 2019 08:19:43 -
>> @@ -53,28 +53,28 @@ int
>>  copy_file(FTSENT *entp, int exists)
>>  {
>>  static char *buf;
>>  static char *zeroes;
>>  struct stat to_stat, *fs;
>>  int from_fd, rcount, rval, to_fd, wcount;
>>  #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
>>  char *p;
>>  #endif
>>  
>> -if (!buf) {
>> +if (buf == NULL) {
>>  buf = malloc(MAXBSIZE);
>> -if (!buf)
>> +if (buf == NULL)
>>  err(1, "malloc");
>>  }
>> -if (!zeroes) {
>> +if (zeroes == NULL) {
>>  zeroes = calloc(1, MAXBSIZE);
>> -if (!zeroes)
>> +if (zeroes == NULL)
>>  err(1, "calloc");
>>  }
>>  
>>  if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
>>  warn("%s", entp->fts_path);
>>  return (1);
>>  }
>>  
>>  fs = entp->fts_statp;
>>  
>> @@ -132,21 +132,21 @@ copy_file(FTSENT *entp, int exists)
>>  if (munmap(p, fs->st_size) == -1) {
>>  warn("%s", entp->fts_path);
>>  rval = 1;
>>  }
>>  }
>>  } else
>>  #endif
>>  {
>>  int skipholes = 0;
>>  struct stat tosb;
>> -if (!fstat(to_fd, ) && S_ISREG(tosb.st_mode))
>> +if (fstat(to_fd, ) == 0 && S_ISREG(tosb.st_mode))
>>  skipholes = 1;
>>  while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
>>  if (skipholes && memcmp(buf, zeroes, rcount) == 0)
>>  wcount = lseek(to_fd, rcount, SEEK_CUR) == -1 ? 
>> -1 : rcount;
>>  else
>>  wcount = write(to_fd, buf, rcount);
>>  if (rcount != wcount || wcount == -1) {
>>  warn("%s", to.p_path);
>>  rval = 1;
>>  break;
>> @@ -169,87 +169,87 @@ copy_file(FTSENT *entp, int exists)
>>  if (pflag && setfile(fs, to_fd))
>>  rval = 1;
>>  /*
>>   * If the source was setuid or setgid, lose the bits unless the
>>   * copy is owned by the same user and group.
>>   */
>>  #define RETAINBITS \
>>  (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
>>  if (!pflag && !exists &&
>>  fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
>> -if (fstat(to_fd, _stat)) {
>> +if (fstat(to_fd, _stat) == -1) {
>>  warn("%s", to.p_path);
>>  rval = 1;
>>  } else if (fs->st_gid == to_stat.st_gid &&
>> -fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
>> +fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask) == -1) {
>>  warn("%s", to.p_path);
>>  rval = 1;
>>  }
>>  }
>>  (void)close(from_fd);
>> -if (close(to_fd)) {
>> +if (close(to_fd) == -1) {
>>  warn("%s", to.p_path);
>>  rval = 1;
>>  }
>>  return (rval);
>>  }
>>  
>>  int
>>  copy_link(FTSENT *p, int exists)
>>  {
>>  int len;
>>  char name[PATH_MAX];
>>  
>>  if (exists && !copy_overwrite())
>>  return (2);
>>  if ((len = readlink(p->fts_path, name, sizeof(name)-1)) == -1) {
>>  

/bin/cp: Incorrect checking the return value

2019-08-16 Thread Masato Asou
Incorrect checking the return value of malloc and system calls in
/bin/cp.

ok?

Index: utils.c
===
RCS file: /cvs/src/bin/cp/utils.c,v
retrieving revision 1.48
diff -u -p -U10 -r1.48 utils.c
--- utils.c 28 Jun 2019 13:34:58 -  1.48
+++ utils.c 16 Aug 2019 08:19:43 -
@@ -53,28 +53,28 @@ int
 copy_file(FTSENT *entp, int exists)
 {
static char *buf;
static char *zeroes;
struct stat to_stat, *fs;
int from_fd, rcount, rval, to_fd, wcount;
 #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
char *p;
 #endif
 
-   if (!buf) {
+   if (buf == NULL) {
buf = malloc(MAXBSIZE);
-   if (!buf)
+   if (buf == NULL)
err(1, "malloc");
}
-   if (!zeroes) {
+   if (zeroes == NULL) {
zeroes = calloc(1, MAXBSIZE);
-   if (!zeroes)
+   if (zeroes == NULL)
err(1, "calloc");
}
 
if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
warn("%s", entp->fts_path);
return (1);
}
 
fs = entp->fts_statp;
 
@@ -132,21 +132,21 @@ copy_file(FTSENT *entp, int exists)
if (munmap(p, fs->st_size) == -1) {
warn("%s", entp->fts_path);
rval = 1;
}
}
} else
 #endif
{
int skipholes = 0;
struct stat tosb;
-   if (!fstat(to_fd, ) && S_ISREG(tosb.st_mode))
+   if (fstat(to_fd, ) == 0 && S_ISREG(tosb.st_mode))
skipholes = 1;
while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
if (skipholes && memcmp(buf, zeroes, rcount) == 0)
wcount = lseek(to_fd, rcount, SEEK_CUR) == -1 ? 
-1 : rcount;
else
wcount = write(to_fd, buf, rcount);
if (rcount != wcount || wcount == -1) {
warn("%s", to.p_path);
rval = 1;
break;
@@ -169,87 +169,87 @@ copy_file(FTSENT *entp, int exists)
if (pflag && setfile(fs, to_fd))
rval = 1;
/*
 * If the source was setuid or setgid, lose the bits unless the
 * copy is owned by the same user and group.
 */
 #defineRETAINBITS \
(S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
if (!pflag && !exists &&
fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) {
-   if (fstat(to_fd, _stat)) {
+   if (fstat(to_fd, _stat) == -1) {
warn("%s", to.p_path);
rval = 1;
} else if (fs->st_gid == to_stat.st_gid &&
-   fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask)) {
+   fchmod(to_fd, fs->st_mode & RETAINBITS & ~myumask) == -1) {
warn("%s", to.p_path);
rval = 1;
}
}
(void)close(from_fd);
-   if (close(to_fd)) {
+   if (close(to_fd) == -1) {
warn("%s", to.p_path);
rval = 1;
}
return (rval);
 }
 
 int
 copy_link(FTSENT *p, int exists)
 {
int len;
char name[PATH_MAX];
 
if (exists && !copy_overwrite())
return (2);
if ((len = readlink(p->fts_path, name, sizeof(name)-1)) == -1) {
warn("readlink: %s", p->fts_path);
return (1);
}
name[len] = '\0';
-   if (exists && unlink(to.p_path)) {
+   if (exists && unlink(to.p_path) == -1) {
warn("unlink: %s", to.p_path);
return (1);
}
-   if (symlink(name, to.p_path)) {
+   if (symlink(name, to.p_path) == -1) {
warn("symlink: %s", name);
return (1);
}
return (pflag ? setfile(p->fts_statp, -1) : 0);
 }
 
 int
 copy_fifo(struct stat *from_stat, int exists)
 {
if (exists && !copy_overwrite())
return (2);
-   if (exists && unlink(to.p_path)) {
+   if (exists && unlink(to.p_path) == -1) {
warn("unlink: %s", to.p_path);
return (1);
}
-   if (mkfifo(to.p_path, from_stat->st_mode)) {
+   if (mkfifo(to.p_path, from_stat->st_mode) == -1) {
warn("mkfifo: %s", to.p_path);
return (1);
}
return (pflag ? setfile(from_stat, -1) : 0);
 }
 
 int
 copy_special(struct stat *from_stat, int exists)
 {
if (exists && !copy_overwrite())
return (2);
-   if (exists && unlink(to.p_path)) {
+   if (exists && unlink(to.p_path) == -1) {

Re: Use `if (retval == -1)' instead of 'if (retval < 0)'

2019-08-14 Thread Masato Asou
Additional information.

From: Masato Asou 
Subject: Use `if (retval == -1)' instead of 'if (retval < 0)'
Date: Wed, 14 Aug 2019 13:42:13 +0900 (JST)

> Hi tech,
> 
> Use `if (retval == -1)' instead of 'if (retval < 0)' when check the
> return value of system call.
> 
> How about it?
> 
> RCS file: /cvs/src/lib/libedit/readline.c,v
> retrieving revision 1.28
> diff -u -p -u -r1.28 readline.c
> --- readline.c  28 Jun 2019 13:32:42 -  1.28
> +++ readline.c  14 Aug 2019 04:38:55 -
> @@ -2112,7 +2112,7 @@ _rl_event_read_char(EditLine *el, wchar_
> return -1;
>  #endif
>  
> -   if (num_read < 0 && errno == EAGAIN)
> +   if (num_read == -1 && errno == EAGAIN)
> continue;
> if (num_read == 0)
> continue;
> --
> ASOU Masato

The valiable num_read has a return value of READ(2) system call as follows:

   2090 while (rl_event_hook) {
   2091
   2092 (*rl_event_hook)();
   2093
   2094 #if defined(FIONREAD)
   2095 if (ioctl(el->el_infd, FIONREAD, ) == -1)
   2096 return -1;
   2097 if (n)
   2098 num_read = read(el->el_infd, , 1);
   2099 else
   2100 num_read = 0;
   2101 #elif defined(F_SETFL) && defined(O_NDELAY)
   2102 if ((n = fcntl(el->el_infd, F_GETFL)) == -1)
   2103 return -1;
   2104 if (fcntl(el->el_infd, F_SETFL, n|O_NDELAY) ==
   -1)
   2105 return -1;
   2106 num_read = read(el->el_infd, , 1);
   2107 if (fcntl(el->el_infd, F_SETFL, n))
   2108 return -1;
   2109 #else
   2110 /* not non-blocking, but what you gonna do? */
   2111 num_read = read(el->el_infd, , 1);
   2112 return -1;
   2113 #endif
   2114
   2115 if (num_read < 0 && errno == EAGAIN)
   2116 continue;
   2117 if (num_read == 0)
   2118 continue;
   2119 break;
   2120 }
--
ASOU Masato



Use `if (retval == -1)' instead of 'if (retval < 0)'

2019-08-13 Thread Masato Asou
Hi tech,

Use `if (retval == -1)' instead of 'if (retval < 0)' when check the
return value of system call.

How about it?

RCS file: /cvs/src/lib/libedit/readline.c,v
retrieving revision 1.28
diff -u -p -u -r1.28 readline.c
--- readline.c  28 Jun 2019 13:32:42 -  1.28
+++ readline.c  14 Aug 2019 04:38:55 -
@@ -2112,7 +2112,7 @@ _rl_event_read_char(EditLine *el, wchar_
return -1;
 #endif
 
-   if (num_read < 0 && errno == EAGAIN)
+   if (num_read == -1 && errno == EAGAIN)
continue;
if (num_read == 0)
continue;
--
ASOU Masato



Re: Why both media and -mediaopt doesn't specify at the same time?

2019-02-14 Thread Masato Asou
From: YASUOKA Masahiko 
Date: Thu, 14 Feb 2019 15:34:26 +0900 (JST)

> On Thu, 14 Feb 2019 08:30:45 +0900 (JST)
> Masato Asou  wrote:
>> From: Claudio Jeker 
>> Date: Wed, 13 Feb 2019 14:25:58 +0100
>> 
>>> On Wed, Feb 13, 2019 at 11:04:02AM +0900, Masato Asou wrote:
>>>> Hi,
>>>> 
>>>> When I execute `ifconfig media XX -mediaopt YY' command, it occured
>>>> error as below.
>>>> 
>>>> $ doas ifconfig em1 media 100baseTX -mediaopt full-duplex
>>>> ifconfig: may not issue both `media' and `-mediaopt'
>>>> $ echo $?
>>>> 1
>>>> 
>>>> Does anyone knows this reason?
>>> 
>>> This was done to prevent changing mediaopt while changing media at the
>>> same time. Media and mediaopt are linked together and ifconfig fetches
>>> the initial settings early on. Changing media and mediaopt at the same
>>> time may end up with a results that puts the interface into an invalid
>>> state.
>> 
>> Thanks for your explanation.
>> 
>> However, media and mediaopt (not `-') can specfy same time. Does this
>> work valid?
> 
> The reason isn't because we can assume all "mediaopt" are cleared when
> changing "media"?  Then clearing individual "mediaopt" when changing
> "media" doesn't make sense.

Oh!
My thought was missing.

Thanks

> 
>>> Is there a particular reason why you need to use media and
>>> -mediaopt at the same time?
>> 
>> I have no particular reason. I just thiking that way.
>> If specified media and -media same time, I only execute one command.
>> 
>>>> I think following patch is works fine.
>>>> 
>>>> $ cvs diff ifconfig.c
>>>> Index: ifconfig.c
>>>> ===
>>>> RCS file: /cvs/src/sbin/ifconfig/ifconfig.c,v
>>>> retrieving revision 1.379
>>>> diff -u -p -r1.379 ifconfig.c
>>>> --- ifconfig.c  30 Sep 2018 18:19:24 -  1.379
>>>> +++ ifconfig.c  13 Feb 2019 01:58:18 -
>>>> @@ -2797,10 +2797,6 @@ unsetmediaopt(const char *val, int d)
>>>> if (actions & A_MEDIAOPTCLR)
>>>> errx(1, "only one `-mediaopt' command may be issued");
>>>> 
>>>> -   /* May not issue `media' and `-mediaopt'. */
>>>> -   if (actions & A_MEDIA)
>>>> -   errx(1, "may not issue both `media' and `-mediaopt'");
>>>> -
>>>> /*
>>>>  * No need to check for A_MEDIAINST, since the test for A_MEDIA
>>>>  * implicitly checks for A_MEDIAINST.
>>>> --
>>>> ASOU Masato
>>>> 
>>> 
>>> -- 
>>> :wq Claudio
>> 
>> --
>> ASOU Masato
>> 
> 
--
ASOU Masato



Re: Why both media and -mediaopt doesn't specify at the same time?

2019-02-13 Thread Masato Asou
From: Claudio Jeker 
Date: Wed, 13 Feb 2019 14:25:58 +0100

> On Wed, Feb 13, 2019 at 11:04:02AM +0900, Masato Asou wrote:
>> Hi,
>> 
>> When I execute `ifconfig media XX -mediaopt YY' command, it occured
>> error as below.
>> 
>> $ doas ifconfig em1 media 100baseTX -mediaopt full-duplex
>> ifconfig: may not issue both `media' and `-mediaopt'
>> $ echo $?
>> 1
>> 
>> Does anyone knows this reason?
> 
> This was done to prevent changing mediaopt while changing media at the
> same time. Media and mediaopt are linked together and ifconfig fetches
> the initial settings early on. Changing media and mediaopt at the same
> time may end up with a results that puts the interface into an invalid
> state.

Thanks for your explanation.

However, media and mediaopt (not `-') can specfy same time. Does this
work valid?

> Is there a particular reason why you need to use media and
> -mediaopt at the same time?

I have no particular reason. I just thiking that way.
If specified media and -media same time, I only execute one command.

>> I think following patch is works fine.
>> 
>> $ cvs diff ifconfig.c
>> Index: ifconfig.c
>> ===
>> RCS file: /cvs/src/sbin/ifconfig/ifconfig.c,v
>> retrieving revision 1.379
>> diff -u -p -r1.379 ifconfig.c
>> --- ifconfig.c  30 Sep 2018 18:19:24 -  1.379
>> +++ ifconfig.c  13 Feb 2019 01:58:18 -
>> @@ -2797,10 +2797,6 @@ unsetmediaopt(const char *val, int d)
>> if (actions & A_MEDIAOPTCLR)
>> errx(1, "only one `-mediaopt' command may be issued");
>> 
>> -   /* May not issue `media' and `-mediaopt'. */
>> -   if (actions & A_MEDIA)
>> -   errx(1, "may not issue both `media' and `-mediaopt'");
>> -
>> /*
>>  * No need to check for A_MEDIAINST, since the test for A_MEDIA
>>  * implicitly checks for A_MEDIAINST.
>> --
>> ASOU Masato
>> 
> 
> -- 
> :wq Claudio

--
ASOU Masato



Why both media and -mediaopt doesn't specify at the same time?

2019-02-12 Thread Masato Asou
Hi,

When I execute `ifconfig media XX -mediaopt YY' command, it occured
error as below.

$ doas ifconfig em1 media 100baseTX -mediaopt full-duplex
ifconfig: may not issue both `media' and `-mediaopt'
$ echo $?
1

Does anyone knows this reason?

I think following patch is works fine.

$ cvs diff ifconfig.c
Index: ifconfig.c
===
RCS file: /cvs/src/sbin/ifconfig/ifconfig.c,v
retrieving revision 1.379
diff -u -p -r1.379 ifconfig.c
--- ifconfig.c  30 Sep 2018 18:19:24 -  1.379
+++ ifconfig.c  13 Feb 2019 01:58:18 -
@@ -2797,10 +2797,6 @@ unsetmediaopt(const char *val, int d)
if (actions & A_MEDIAOPTCLR)
errx(1, "only one `-mediaopt' command may be issued");

-   /* May not issue `media' and `-mediaopt'. */
-   if (actions & A_MEDIA)
-   errx(1, "may not issue both `media' and `-mediaopt'");
-
/*
 * No need to check for A_MEDIAINST, since the test for A_MEDIA
 * implicitly checks for A_MEDIAINST.
--
ASOU Masato



Re: SEGV was occurred in libedit

2018-10-10 Thread Masato Asou
Hi,

From: YASUOKA Masahiko 
Date: Wed, 10 Oct 2018 13:46:10 +0900 (JST)

> initialize new line.
> 
> See #1086, "lastline" is set to previous lastline.  Then new line is not 
> initialized.
> 
> I think the following diff is better since it initializes the line in
> the same way for first line.
> 
> diff --git a/lib/libedit/refresh.c b/lib/libedit/refresh.c
> index f2d001d67cb..9bf46faa777 100644
> --- a/lib/libedit/refresh.c
> +++ b/lib/libedit/refresh.c
> @@ -1083,7 +1083,7 @@ re_fastputc(EditLine *el, wint_t c)
>   el->el_display[i - 1] = lastline;
>   } else {
>   el->el_cursor.v++;
> - lastline = el->el_display[el->el_refresh.r_oldcv++];
> + lastline = el->el_display[++el->el_refresh.r_oldcv];
>   }
>   re__copy_and_pad(lastline, L"", el->el_terminal.t_size.h);
>  

I confirmed this patch. I think that this patch is more crrect then my
patch.

thanks.
--
ASOU Masato



SEGV was occurred in libedit

2018-10-09 Thread Masato Asou
Hi,

When I use /usr/bin/bc command with MALLOC_OPTIONS=UJ, SEGV was
occurred in libedit.

$ MALLOC_OPTIONS=UJ /usr/bin/bc
10 + 20 + 30 + 40 + 50 + 60 + 70 + 80 + 90 
+ Segmentation fault (core dumped)

$ /usr/bin/gdb /usr/bin/bc bc.core
GNU gdb 6.3

... snip ...

(no debugging symbols found)

Core was generated by `bc'.
Program terminated with signal 11, Segmentation fault.
(no debugging symbols found)
Loaded symbols for /usr/bin/bc
Reading symbols from /usr/lib/libedit.so.5.2...done.
Loaded symbols for /usr/lib/libedit.so.5.2
Reading symbols from /usr/lib/libcurses.so.14.0...done.
Loaded symbols for /usr/lib/libcurses.so.14.0
Reading symbols from /usr/lib/libcrypto.so.43.1...done.
Loaded symbols for /usr/lib/libcrypto.so.43.1
Reading symbols from /usr/lib/libc.so.92.3...done.
Loaded symbols for /usr/lib/libc.so.92.3
Reading symbols from /usr/libexec/ld.so...done.
Loaded symbols for /usr/libexec/ld.so
#0  re_refresh (el=Variable "el" is not available.
) at /home/asou/work/current/src/lib/libedit/refresh.c:518
518 while (*o)
(gdb) bt
#0  re_refresh (el=Variable "el" is not available.
) at /home/asou/work/current/src/lib/libedit/refresh.c:518
#1  0x0fb7c886a19e in el_wgets (el=0xfb82e53d800, nread=Variable
"nread" is not available.
)
at /home/asou/work/current/src/lib/libedit/read.c:372
#2  0x0fb7c886a533 in el_gets (el=0xfb82e53d800,
nread=0x7f7c734c)
at /home/asou/work/current/src/lib/libedit/eln.c:74
#3  0x0fb5bf407aa1 in ?? () from /usr/bin/bc
#4  0x0fb5bf40661a in ?? () from /usr/bin/bc
#5  0x0fb5bf4013f4 in ?? () from /usr/bin/bc
#6  0x0fb5bf4009d6 in ?? () from /usr/bin/bc
#7  0x in ?? ()
Current language:  auto; currently minimal
(gdb) 

I enter 83 characters that extend beyond a single line as follows and
delete last '0' by DEL key, then SEGV was occurred.

10 + 20 + 30 + 40 + 50 + 60 + 70 + 80 + 90 
+ 10

I made following patch and it works fine!

ok?

diff --git a/lib/libedit/refresh.c b/lib/libedit/refresh.c
index f2d001d67cb..5f2d607d356 100644
--- a/lib/libedit/refresh.c
+++ b/lib/libedit/refresh.c
@@ -1061,6 +1061,7 @@ re_fastputc(EditLine *el, wint_t c)

terminal__putc(el, c);
el->el_display[el->el_cursor.v][el->el_cursor.h++] = c;
+   el->el_display[el->el_cursor.v][el->el_cursor.h] = '\0';
while (--w > 0)
el->el_display[el->el_cursor.v][el->el_cursor.h++]
= MB_FILL_CHAR;
--
ASOU Masato



Next command of GDB does not work properly unusual

2018-06-28 Thread Masato Asou
Hi tech,

I have a problem and I have ad hoc patch to solve this problem.
However, this patch is AD HOC. Does anybody have correct solution?

The next command of GDB does not work properly.
I use OpenBSD 6.3 and /usr/bin/gdb (GDB 6.3).

When I debuging my program with next command, I expect stop at next
line. However, program returned from current function unusual. 

It seems that this problem will occur if there is a WHILE immediately
after the function call.

Example as below:

$ uname -a
OpenBSD asou-obsd63.soum.co.jp 6.3 GENERIC#100 amd64
$ cat main.c
#include 
#include 
#include 
#include 

int
main(int argc, char *argv[])
{
extern void sub(int);
int ch;
int flag_a = 0;

sub(1);
while ((ch = getopt(argc, argv, "a")) != -1) {
switch (ch) {
case 'a':
flag_a = 1;
break;
default:
err(1, "Unknown option: %c\n", ch);
break;
}
}

printf("flag_a = %d\n", flag_a);
return (0);
}

void
sub(int flag)
{
printf("flag = %d\n", flag);
}
$ cc -g main.c
$ gdb a.out
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "amd64-unknown-openbsd6.3"...
(gdb) b main
Breakpoint 1 at 0x56b: file main.c, line 11.
(gdb) run
Starting program: /home/asou/tmp/a.out 
Breakpoint 1 at 0x12776150056b: file main.c, line 11.

Breakpoint 1, main (argc=1, argv=0x7f7c3b08) at main.c:11
11  int flag_a = 0;
Current language:  auto; currently minimal
(gdb) next
13  sub(1);
(gdb) next
flag = 1
0x127761500579 in main (argc=1, argv=0x7f7c3b08)
   from /home/asou/tmp/a.out
(gdb) next
Single stepping until exit from function main, 
which has no line number information.
flag_a = 0
0x127761500436 in _start () from /home/asou/tmp/a.out
(gdb) bt
#0  0x127761500436 in _start () from /home/asou/tmp/a.out
#1  0x in ?? ()
(gdb) 

My patch as below:

$ git diff
diff --git a/gnu/usr.bin/binutils/gdb/dwarf2read.c 
b/gnu/usr.bin/binutils/gdb/dwarf2read.c
index 96f9e0f8551..651904d763d 100644
--- a/gnu/usr.bin/binutils/gdb/dwarf2read.c
+++ b/gnu/usr.bin/binutils/gdb/dwarf2read.c
@@ -6479,7 +6479,11 @@ dwarf_decode_lines (struct line_header *lh, char 
*comp_dir, bfd *abfd,
* lh->minimum_instruction_length;
  line += lh->line_base + (adj_opcode % lh->line_range);
   lh->file_names[file - 1].included_p = 1;
+#if 0 /* for debug by asou */
   if (!decode_for_pst_p)
+#else /* for debug by asou */
+  if (line != 0 && !decode_for_pst_p)
+#endif /* for debug by asou */
 {
  /* Append row to matrix using current values.  */
  record_line (current_subfile, line, 
--
ASOU Masato