Skip to content

Commit 9c20755

Browse files
committed
Fix some strcpy() buffer overrrun issues
1 parent 2990c00 commit 9c20755

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

Changes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Revision history for the Perl extension Win32.
22

3+
0.43 [2011-01-12]
4+
- fix a few potential buffer overrun bugs reported by Alex Davies.
5+
[perl#78710]
6+
37
0.42 [2011-01-06]
48
- remove brittle test for Win32::GetLongPathName($ENV{SYSTEMROOT})
59
which will fail if the case of the environment value doesn't

META.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--- #YAML:1.0
22
name: Win32
33
abstract: Interfaces to some Win32 API Functions
4-
version: 0.42
4+
version: 0.43
55
author:
66
- Jan Dubois <[email protected]>
77
license: perl

Win32.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package Win32;
88
require DynaLoader;
99

1010
@ISA = qw|Exporter DynaLoader|;
11-
$VERSION = '0.42';
11+
$VERSION = '0.43';
1212
$XS_VERSION = $VERSION;
1313
$VERSION = eval $VERSION;
1414

Win32.xs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,8 @@ XS(w32_GetFullPathName)
14831483
/* fullname is the MAX_PATH+1 sized buffer returned from PerlDir_mapA()
14841484
* or the 2*MAX_PATH sized local buffer in the __CYGWIN__ case.
14851485
*/
1486-
strcpy(lastchar+1, "\\");
1486+
if (lastchar - fullname < MAX_PATH - 1)
1487+
strcpy(lastchar+1, "\\");
14871488
}
14881489
}
14891490

@@ -1519,13 +1520,15 @@ XS(w32_GetLongPathName)
15191520
WCHAR wide_path[MAX_PATH+1];
15201521
WCHAR *long_path;
15211522

1522-
wcscpy(wide_path, wstr);
1523-
Safefree(wstr);
1524-
long_path = my_longpathW(wide_path);
1525-
if (long_path) {
1526-
ST(0) = wstr_to_sv(aTHX_ long_path);
1527-
XSRETURN(1);
1523+
if (wcslen(wstr) < countof(wide_path)) {
1524+
wcscpy(wide_path, wstr);
1525+
long_path = my_longpathW(wide_path);
1526+
if (long_path) {
1527+
ST(0) = wstr_to_sv(aTHX_ long_path);
1528+
XSRETURN(1);
1529+
}
15281530
}
1531+
Safefree(wstr);
15291532
}
15301533
else {
15311534
SV *path;
@@ -1535,11 +1538,13 @@ XS(w32_GetLongPathName)
15351538

15361539
path = ST(0);
15371540
pathstr = SvPV(path,len);
1538-
strcpy(tmpbuf, pathstr);
1539-
pathstr = my_longpathA(tmpbuf);
1540-
if (pathstr) {
1541-
ST(0) = sv_2mortal(newSVpvn(pathstr, strlen(pathstr)));
1542-
XSRETURN(1);
1541+
if (len < sizeof(tmpbuf)) {
1542+
strcpy(tmpbuf, pathstr);
1543+
pathstr = my_longpathA(tmpbuf);
1544+
if (pathstr) {
1545+
ST(0) = sv_2mortal(newSVpvn(pathstr, strlen(pathstr)));
1546+
XSRETURN(1);
1547+
}
15431548
}
15441549
}
15451550
XSRETURN_EMPTY;
@@ -1572,14 +1577,19 @@ XS(w32_CopyFile)
15721577
{
15731578
dXSARGS;
15741579
BOOL bResult;
1580+
char *pszSourceFile;
15751581
char szSourceFile[MAX_PATH+1];
15761582

15771583
if (items != 3)
15781584
Perl_croak(aTHX_ "usage: Win32::CopyFile($from, $to, $overwrite)");
1579-
strcpy(szSourceFile, PerlDir_mapA(SvPV_nolen(ST(0))));
1580-
bResult = CopyFileA(szSourceFile, PerlDir_mapA(SvPV_nolen(ST(1))), !SvTRUE(ST(2)));
1581-
if (bResult)
1582-
XSRETURN_YES;
1585+
1586+
pszSourceFile = PerlDir_mapA(SvPV_nolen(ST(0)));
1587+
if (strlen(pszSourceFile) < sizeof(szSourceFile)) {
1588+
strcpy(szSourceFile, pszSourceFile);
1589+
bResult = CopyFileA(szSourceFile, PerlDir_mapA(SvPV_nolen(ST(1))), !SvTRUE(ST(2)));
1590+
if (bResult)
1591+
XSRETURN_YES;
1592+
}
15831593
XSRETURN_NO;
15841594
}
15851595

0 commit comments

Comments
 (0)