Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POSIX::read and POSIX::write return -1 on error #16347

Open
p5pRT opened this issue Jan 2, 2018 · 6 comments
Open

POSIX::read and POSIX::write return -1 on error #16347

p5pRT opened this issue Jan 2, 2018 · 6 comments

Comments

@p5pRT
Copy link

p5pRT commented Jan 2, 2018

Migrated from rt.perl.org#132677 (status was 'open')

Searchable as RT132677$

@p5pRT
Copy link
Author

p5pRT commented Jan 2, 2018

From @zmanda-pusher1

Created by @zmanda-pusher1

POSIX​::read return -1 on some error, the documentation say it return undef.
The bug affect many POSIX function.
This bug affect all programs that check only for undef and not -1 for error.

Simple program to reproduce​:

#!/usr/bin/perl -W
#
use POSIX;
my $buf;
my $count = POSIX​::read(-1, $buf, 1);

print "count​: $count\n";
exit;

The bug is in 5.24.0 and newer.
I think the bogus commit is​: ddc7c5c
which return -1 when the fd is negative
I think a fix could be to change the
  XSRETURN_IV(-1);
to something that return undef in the T_FD case of ext/POSIX/typemap

Perl Info

Flags:
    category=library
    severity=critical
    module=POSIX

Site configuration information for perl 5.26.1:

Configured by martinea at Tue Jan  2 14:59:19 EST 2018.

Summary of my perl5 (revision 5 version 26 subversion 1) configuration:
  Commit id: 714a461a2ab3017f19ea0f7bbb4934b2309d3aa8
  Platform:
    osname=linux
    osvers=4.11.12-100.fc24.x86_64
    archname=x86_64-linux
    uname='linux localhost.localdomain 4.11.12-100.fc24.x86_64 #1 smp fri
jul 21 17:35:20 utc 2017 x86_64 x86_64 x86_64 gnulinux '
    config_args='-des -Dprefix=/home/martinea/localperl -Dusedevel'
    hint=recommended
    useposix=true
    d_sigaction=define
    useithreads=undef
    usemultiplicity=undef
    use64bitint=define
    use64bitall=define
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
    bincompat5005=undef
  Compiler:
    cc='cc'
    ccflags ='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong
-I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
-D_FORTIFY_SOURCE=2'
    optimize='-O2'
    cppflags='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong
-I/usr/local/include'
    ccversion=''
    gccversion='6.3.1 20161221 (Red Hat 6.3.1-1)'
    gccosandvers=''
    intsize=4
    longsize=8
    ptrsize=8
    doublesize=8
    byteorder=12345678
    doublekind=3
    d_longlong=define
    longlongsize=8
    d_longdbl=define
    longdblsize=16
    longdblkind=3
    ivtype='long'
    ivsize=8
    nvtype='double'
    nvsize=8
    Off_t='off_t'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='cc'
    ldflags =' -fstack-protector-strong -L/usr/local/lib'
    libpth=/usr/local/lib /usr/lib /lib/../lib64 /usr/lib/../lib64 /lib
/lib64 /usr/lib64 /usr/local/lib64
    libs=-lpthread -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc
-lgdbm_compat
    perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
    libc=libc-2.23.so
    so=so
    useshrplib=false
    libperl=libperl.a
    gnulibc_version='2.23'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs
    dlext=so
    d_dlsymun=undef
    ccdlflags='-Wl,-E'
    cccdlflags='-fPIC'
    lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector-strong'



@INC for perl 5.26.1:
    /home/martinea/perl5/lib/perl5
    /home/martinea/perl5/lib/perl5/x86_64-linux-thread-multi
    /home/martinea/perl5/lib/perl5
    /home/martinea/perl5/lib/perl5/x86_64-linux-thread-multi
    /home/martinea/localperl/lib/site_perl/5.26.1/x86_64-linux
    /home/martinea/localperl/lib/site_perl/5.26.1
    /home/martinea/localperl/lib/5.26.1/x86_64-linux
    /home/martinea/localperl/lib/5.26.1
    /home/martinea/localperl/lib/site_perl


Environment for perl 5.26.1:
    HOME=/home/martinea
    LANG=en_CA.UTF-8
    LANGUAGE=
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)

PATH=/home/martinea/perl5/bin:/amanda/h1/linux/sbin:/home/martinea/perl5/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/home/martinea/.local/bin:/home/martinea/bin

PERL5LIB=/home/martinea/perl5/lib/perl5:/home/martinea/perl5/lib/perl5/x86_64-linux-thread-multi:/home/martinea/perl5/lib/perl5:/home/martinea/perl5/lib/perl5/x86_64-linux-thread-multi
    PERLLIB=/amanda/h1/linux/lib/amanda/perl
    PERL_BADLANG (unset)
    PERL_LOCAL_LIB_ROOT=/home/martinea/perl5:/home/martinea/perl5
    PERL_MB_OPT=--install_base "/home/martinea/perl5"
    PERL_MM_OPT=INSTALL_BASE=/home/martinea/perl5
    SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Jan 2, 2018

From @jkeenan

On Tue, 02 Jan 2018 21​:09​:06 GMT, martineau.jl@​gmail.com wrote​:

This is a bug report for perl from martineau.jl@​gmail.com,
generated with the help of perlbug 1.40 running under perl 5.26.1.

-----------------------------------------------------------------
[Please describe your issue here]

POSIX​::read return -1 on some error, the documentation say it return
undef.
The bug affect many POSIX function.
This bug affect all programs that check only for undef and not -1 for
error.

Simple program to reproduce​:

#!/usr/bin/perl -W
#
use POSIX;
my $buf;
my $count = POSIX​::read(-1, $buf, 1);

print "count​: $count\n";
exit;

The bug is in 5.24.0 and newer.
I think the bogus commit is​: ddc7c5c
which return -1 when the fd is negative
I think a fix could be to change the
XSRETURN_IV(-1);
to something that return undef in the T_FD case of ext/POSIX/typemap

Requesting comment from committer.

--
James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Jan 2, 2018

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Jan 3, 2018

From @jhi

Duh. How about the attached patch?

@p5pRT
Copy link
Author

p5pRT commented Jan 3, 2018

From @jhi

0001-perl-132677-POSIX-read-and-POSIX-write-return-1-on-e.patch
From 8053c874e4b4c64370cc07115afdc46cfd8e6b53 Mon Sep 17 00:00:00 2001
From: Jarkko Hietaniemi <jhi@iki.fi>
Date: Wed, 3 Jan 2018 09:25:45 +0200
Subject: [PATCH] [perl #132677] POSIX::read and POSIX::write return -1 on
 error

ddc7c5c7d33132a836845f632085f65497425023 wrongly did XSRETURN_IV(-1)
when it should have done XSRETURN_UNDEF.
---
 ext/POSIX/t/posix.t | 4 +++-
 ext/POSIX/typemap   | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/ext/POSIX/t/posix.t b/ext/POSIX/t/posix.t
index 1b2dd4010b..a442ccc544 100644
--- a/ext/POSIX/t/posix.t
+++ b/ext/POSIX/t/posix.t
@@ -10,7 +10,7 @@ BEGIN {
     require 'loc_tools.pl';
 }
 
-use Test::More tests => 93;
+use Test::More tests => 94;
 
 use POSIX qw(fcntl_h signal_h limits_h _exit getcwd open read strftime write
 	     errno localeconv dup dup2 lseek access);
@@ -376,6 +376,8 @@ is($buffer, "# Ex", 'read');
     undef $buffer;
     read($fd2, $buffer, 4) if $fd2 > 2;
     is($buffer, "# Ex", 'read');
+
+    is(read(-1, $buffer, 1), undef, 'read from bad fd');
 }
 
 # The FreeBSD man page warns:
diff --git a/ext/POSIX/typemap b/ext/POSIX/typemap
index 753afcd1a5..4808a7fdb1 100644
--- a/ext/POSIX/typemap
+++ b/ext/POSIX/typemap
@@ -39,7 +39,7 @@ T_SIGNO
 T_FD
 	if ((fd = (int)SvIV($arg)) < 0) {
 	     SETERRNO(EBADF, RMS_IFI);
-	     XSRETURN_IV(-1);
+	     XSRETURN_UNDEF;
 	}
 
 OUTPUT
-- 
2.14.3

@p5pRT
Copy link
Author

p5pRT commented Jan 3, 2018

From @Leont

On Wed, Jan 3, 2018 at 9​:01 AM, Jarkko Hietaniemi <jhi@​iki.fi> wrote​:

Duh. How about the attached patch?

This looks good to me. Some of those functions aren't documented to
return undef on error yet, and for tcgetpgrp this results in
inconsistent behavior (returning undef on some errors but -1 on
others), I would argue it should return undef on all errors.

Leont

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants