>Synopsis:      Perl module OpenBSD::MkTemp. Unexpected behaviour in scalar 
>context.
>Category:      library
>Environment:
        System      : OpenBSD 5.9
        Details     : OpenBSD 5.9 (GENERIC.MP) #1: Mon May  2 19:06:55 CEST 2016
                         
france...@avalon.thwn:/usr/src/sys/arch/amd64/compile/GENERIC.MP

        Architecture: OpenBSD.amd64
        Machine     : amd64
>Description:
        When OpenBSD::MkTemp::mkstemp and OpenBSD::MkTemp::mkstemps are
        called in scalar context, they return the filepath string.
        They're expected to return the newly created filehandle instead.
        In list context they return the new filehandle and filepath as
        documented.

        OpenBSD::MkTemp is used/required by those modules:
        /usr/src/usr.sbin/pkg_add/OpenBSD/PkgCreate.pm
        /usr/src/usr.sbin/pkg_add/OpenBSD/Temp.pm

        OpenBSD::Temp is used/required by those modules:
        /usr/src/usr.sbin/pkg_add/OpenBSD/Add.pm
        /usr/src/usr.sbin/pkg_add/OpenBSD/Delete.pm
        /usr/src/usr.sbin/pkg_add/OpenBSD/PackageLocation.pm
        /usr/src/usr.sbin/pkg_add/OpenBSD/PackageRepository.pm
        /usr/src/usr.sbin/pkg_add/OpenBSD/PkgCreate.pm
        /usr/src/usr.sbin/pkg_add/OpenBSD/PkgSign.pm

        It seems OpenBSD::Temp::mkstemp is called in scalar context
        just in /usr/src/usr.sbin/pkg_add/OpenBSD/PkgSign.pm;
        everywhere else they're called in list context.

>How-To-Repeat:
        Basic demo code:

        # mktemptest.pl

        use strict;
        use warnings;
        use OpenBSD::MkTemp qw( mkstemp );
        use Data::Dumper;

        my $fh = OpenBSD::MkTemp::mkstemp("/tmp/msg_XXXXXXXXXX");
        print Dumper($fh);

        # EOF
        
        $ perl mktemptest.pl
        $VAR1 = '/tmp/msg_RrFGcguwnL';


>Fix:

        Patch follows. Tested in both scalar and list contexts. I played
        with pkg_add and pkg_delete too and they seem to work.

 
--- MkTemp.pm.orig      Thu May  5 18:16:11 2016
+++ MkTemp.pm   Thu May  5 19:06:20 2016
@@ -17,7 +17,7 @@
 {
        my $template = shift;
        my $fh = mkstemps_real($template, 0);
-       return $fh && ($fh, $template)
+       return wantarray() ? ($fh, $template) : $fh;
 }
 
 sub mkstemps($$)
@@ -25,7 +25,7 @@
        my($template, $suffix) = @_;
        $template .= $suffix;
        my $fh = mkstemps_real($template, length($suffix));
-       return $fh && ($fh, $template)
+       return wantarray() ? ($fh, $template) : $fh;
 }
 
 


dmesg:
OpenBSD 5.9 (GENERIC.MP) #1: Mon May  2 19:06:55 CEST 2016
    france...@avalon.thwn:/usr/src/sys/arch/amd64/compile/GENERIC.MP
real mem = 3203334144 (3054MB)
avail mem = 3102113792 (2958MB)
mpath0 at root
scsibus0 at mpath0: 256 targets
mainbus0 at root
bios0 at mainbus0: SMBIOS rev. 2.4 @ 0xf0000 (31 entries)
bios0: vendor Phoenix Technologies, LTD version "5.13" date 10/29/2007
bios0: HP-Pavilion KA869AA-ABZ a6335.it
acpi0 at bios0: rev 2
acpi0: sleep states S0 S1 S3 S4 S5
acpi0: tables DSDT FACP SLIC SSDT HPET MCFG APIC
acpi0: wakeup devices HUB0(S5) XVR0(S5) XVR1(S5) PS2M(S4) PS2K(S4) USB0(S3) 
USB2(S3) AZAD(S5) MMAC(S5)
acpitimer0 at acpi0: 3579545 Hz, 24 bits
acpihpet0 at acpi0: 25000000 Hz
acpimcfg0 at acpi0 addr 0xf0000000, bus 0-63
acpimadt0 at acpi0 addr 0xfee00000: PC-AT compat
cpu0 at mainbus0: apid 0 (boot processor)
cpu0: AMD Athlon(tm) 64 X2 Dual Core Processor 5000+, 2612.28 MHz
cpu0: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,HTT,SSE3,CX16,NXE,MMXX,FFXSR,LONG,3DNOW2,3DNOW,LAHF,CMPLEG,SVM,EAPICSP,AMCR8,3DNOWP
cpu0: 64KB 64b/line 2-way I-cache, 64KB 64b/line 2-way D-cache, 512KB 64b/line 
16-way L2 cache
cpu0: ITLB 32 4KB entries fully associative, 8 4MB entries fully associative
cpu0: DTLB 32 4KB entries fully associative, 8 4MB entries fully associative
mtrr: Pentium Pro MTRR support, 8 var ranges, 88 fixed ranges
cpu0: apic clock running at 200MHz
cpu1 at mainbus0: apid 1 (application processor)
cpu1: AMD Athlon(tm) 64 X2 Dual Core Processor 5000+, 2611.89 MHz
cpu1: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,HTT,SSE3,CX16,NXE,MMXX,FFXSR,LONG,3DNOW2,3DNOW,LAHF,CMPLEG,SVM,EAPICSP,AMCR8,3DNOWP
cpu1: 64KB 64b/line 2-way I-cache, 64KB 64b/line 2-way D-cache, 512KB 64b/line 
16-way L2 cache
cpu1: ITLB 32 4KB entries fully associative, 8 4MB entries fully associative
cpu1: DTLB 32 4KB entries fully associative, 8 4MB entries fully associative
ioapic0 at mainbus0: apid 2 pa 0xfec00000, version 11, 24 pins
ioapic0: misconfigured as apic 0, remapped to apid 2
acpiprt0 at acpi0: bus 0 (PCI0)
acpiprt1 at acpi0: bus 1 (HUB0)
acpicpu0 at acpi0: C1(@1 halt!), PSS
acpicpu1 at acpi0: C1(@1 halt!), PSS
acpitz0 at acpi0: critical temperature is 75 degC
acpibtn0 at acpi0: PWRB
cpu0: PowerNow! K8 2612 MHz: speeds: 2600 2400 2200 2000 1800 1000 MHz
pci0 at mainbus0 bus 0
"NVIDIA MCP61 Memory" rev 0xa1 at pci0 dev 0 function 0 not configured
pcib0 at pci0 dev 1 function 0 "NVIDIA MCP61 ISA" rev 0xa2
nviic0 at pci0 dev 1 function 1 "NVIDIA MCP61 SMBus" rev 0xa2
iic0 at nviic0
spdmem0 at iic0 addr 0x50: 1GB DDR2 SDRAM non-parity PC2-5300CL5
spdmem1 at iic0 addr 0x51: 1GB DDR2 SDRAM non-parity PC2-5300CL5
spdmem2 at iic0 addr 0x52: 512MB DDR2 SDRAM non-parity PC2-5300CL5
spdmem3 at iic0 addr 0x53: 512MB DDR2 SDRAM non-parity PC2-5300CL5
iic1 at nviic0
"NVIDIA MCP61 Memory" rev 0xa2 at pci0 dev 1 function 2 not configured
ohci0 at pci0 dev 2 function 0 "NVIDIA MCP61 USB" rev 0xa3: apic 2 int 11, 
version 1.0, legacy support
ehci0 at pci0 dev 2 function 1 "NVIDIA MCP61 USB" rev 0xa3: apic 2 int 7
usb0 at ehci0: USB revision 2.0
uhub0 at usb0 "NVIDIA EHCI root hub" rev 2.00/1.00 addr 1
ppb0 at pci0 dev 4 function 0 "NVIDIA MCP61" rev 0xa1
pci1 at ppb0 bus 1
azalia0 at pci0 dev 5 function 0 "NVIDIA MCP61 HD Audio" rev 0xa2: apic 2 int 11
azalia0: codecs: Realtek ALC888
audio0 at azalia0
pciide0 at pci0 dev 6 function 0 "NVIDIA MCP61 IDE" rev 0xa2: DMA, channel 0 
configured to compatibility, channel 1 configured to compatibility
pciide0: channel 0 disabled (no drives)
pciide0: channel 1 ignored (disabled)
nfe0 at pci0 dev 7 function 0 "NVIDIA MCP61 LAN" rev 0xa2: apic 2 int 5, 
address 00:1e:8c:6e:8b:50
rlphy0 at nfe0 phy 1: RTL8201L 10/100 PHY, rev. 1
pciide1 at pci0 dev 8 function 0 "NVIDIA MCP61 SATA" rev 0xa2: DMA
pciide1: using apic 2 int 11 for native-PCI interrupt
wd0 at pciide1 channel 0 drive 0: <WDC WD5000AAKS-65YGA0>
wd0: 16-sector PIO, LBA48, 476940MB, 976773168 sectors
wd0(pciide1:0:0): using PIO mode 4, Ultra-DMA mode 6
atapiscsi0 at pciide1 channel 1 drive 0
scsibus1 at atapiscsi0: 2 targets
cd0 at scsibus1 targ 0 lun 0: <ATAPI, DVD A DH16A3L, 8H1F> ATAPI 5/cdrom 
removable
cd0(pciide1:1:0): using PIO mode 4, Ultra-DMA mode 5
pciide2 at pci0 dev 8 function 1 "NVIDIA MCP61 SATA" rev 0xa2: DMA
pciide2: using apic 2 int 10 for native-PCI interrupt
ppb1 at pci0 dev 9 function 0 "NVIDIA MCP61 PCIE" rev 0xa2
pci2 at ppb1 bus 2
radeondrm0 at pci2 dev 0 function 0 "ATI Radeon HD 2400 Pro" rev 0x00
drm0 at radeondrm0
radeondrm0: apic 2 int 7
ppb2 at pci0 dev 11 function 0 "NVIDIA MCP61 PCIE" rev 0xa2
pci3 at ppb2 bus 3
pchb0 at pci0 dev 24 function 0 "AMD AMD64 0Fh HyperTransport" rev 0x00
pchb1 at pci0 dev 24 function 1 "AMD AMD64 0Fh Address Map" rev 0x00
pchb2 at pci0 dev 24 function 2 "AMD AMD64 0Fh DRAM Cfg" rev 0x00
kate0 at pci0 dev 24 function 3 "AMD AMD64 0Fh Misc Cfg" rev 0x00: core rev 
BH-G2
isa0 at pcib0
isadma0 at isa0
pckbc0 at isa0 port 0x60/5 irq 1 irq 12
pckbd0 at pckbc0 (kbd slot)
wskbd0 at pckbd0: console keyboard
pms0 at pckbc0 (aux slot)
wsmouse0 at pms0 mux 0
pcppi0 at isa0 port 0x61
spkr0 at pcppi0
usb1 at ohci0: USB revision 1.0
uhub1 at usb1 "NVIDIA OHCI root hub" rev 1.00/1.00 addr 1
rsu0 at uhub0 port 4 configuration 1 interface 0 "Manufacturer Realtek ASUS EZ 
N Network Adapter" rev 2.00/2.00 addr 2
rsu0: MAC/BB RTL8712 cut 3, address 74:d0:2b:ce:83:4c
rum0 at uhub0 port 6 configuration 1 interface 0 "Ralink 802.11 bg WLAN" rev 
2.00/0.01 addr 3
rum0: MAC/BBP RT2573 (rev 0x2573a), RF RT2528, address 00:19:5b:cc:97:87
umass0 at uhub0 port 8 configuration 1 interface 0 "Generic Mass Storage 
Device" rev 2.00/1.00 addr 4
umass0: using SCSI over Bulk-Only
scsibus2 at umass0: 2 targets, initiator 0
sd0 at scsibus2 targ 1 lun 0: <Generic, USB SD Reader, 1.00> SCSI0 0/direct 
removable
sd1 at scsibus2 targ 1 lun 1: <Generic, USB CF Reader, 1.01> SCSI0 0/direct 
removable
sd2 at scsibus2 targ 1 lun 2: <Generic, USB SM Reader, 1.02> SCSI0 0/direct 
removable
sd3 at scsibus2 targ 1 lun 3: <Generic, USB MS Reader, 1.03> SCSI0 0/direct 
removable
vscsi0 at root
scsibus3 at vscsi0: 256 targets
softraid0 at root
scsibus4 at softraid0: 256 targets
root on wd0a (049b54656547ae98.a) swap on wd0b dump on wd0b
radeondrm0: 1440x900
wsdisplay0 at radeondrm0 mux 1: console (std, vt100 emulation), using wskbd0
wsdisplay0: screen 1-5 added (std, vt100 emulation)

usbdevs:
Controller /dev/usb0:
addr 1: high speed, self powered, config 1, EHCI root hub(0x0000), 
NVIDIA(0x10de), rev 1.00
 port 1 powered
 port 2 powered
 port 3 powered
 port 4 addr 2: high speed, power 500 mA, config 1, ASUS EZ N Network 
Adapter(0x1786), Manufacturer Realtek(0x0b05), rev 2.00, iSerialNumber 
00e04c000001
 port 5 powered
 port 6 addr 3: high speed, power 300 mA, config 1, 802.11 bg WLAN(0x3c03), 
Ralink(0x07d1), rev 0.01
 port 7 powered
 port 8 addr 4: high speed, power 100 mA, config 1, Mass Storage 
Device(0x6377), Generic(0x058f), rev 1.00, iSerialNumber 920321111113
 port 9 powered
 port 10 powered
Controller /dev/usb1:
addr 1: full speed, self powered, config 1, OHCI root hub(0x0000), 
NVIDIA(0x10de), rev 1.00
 port 1 powered
 port 2 powered
 port 3 powered
 port 4 powered
 port 5 powered
 port 6 powered
 port 7 powered
 port 8 powered
 port 9 powered
 port 10 powered

Reply via email to