8000 [2.7] bpo-1104: msilib.SummaryInfo.GetProperty() truncates the string… · python/cpython@d5409eb · GitHub
[go: up one dir, main page]

Skip to content

Commit d5409eb

Browse files
uranusjrmiss-islington
authored andcommitted
[2.7] bpo-1104: msilib.SummaryInfo.GetProperty() truncates the string by one character (GH-4517) (GH-11749)
Add one char to MsiSummaryInfoGetProperty() output Based on the patch in [bpo-1104](https://bugs.python.org/issue1104) by Anthony Tuininga (atuining) and Mark McMahon (markm) (cherry picked from commit 2de576e) Co-authored-by: Tzu-ping Chung <uranusjr@gmail.com> https://bugs.python.org/issue1104
1 parent 49778ad commit d5409eb

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

Lib/test/test_msilib.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
11
""" Test suite for the code in msilib """
22
import unittest
3-
import os
4-
from test_support import run_unittest, import_module
3+
from test_support import TESTFN, import_module, run_unittest, unlink
54
msilib = import_module('msilib')
5+
import msilib.schema
6+
7+
8+
def init_database():
9+
path = TESTFN + '.msi'
10+
db = msilib.init_database(
11+
path,
12+
msilib.schema,
13+
'Python Tests',
14+
'product_code',
15+
'1.0',
16+
'PSF',
17+
)
18+
return db, path
19+
20+
21+
class MsiDatabaseTestCase(unittest.TestCase):
22+
23+
def test_summaryinfo_getproperty_issue1104(self):
24+
db, db_path = init_database()
25+
try:
26+
sum_info = db.GetSummaryInformation(99)
27+
title = sum_info.GetProperty(msilib.PID_TITLE)
28+
self.assertEqual(title, b"Installation Database")
29+
30+
sum_info.SetProperty(msilib.PID_TITLE, "a" * 999)
31+
title = sum_info.GetProperty(msilib.PID_TITLE)
32+
self.assertEqual(title, b"a" * 999)
33+
34+
sum_info.SetProperty(msilib.PID_TITLE, "a" * 1000)
35+
title = sum_info.GetProperty(msilib.PID_TITLE)
36+
self.assertEqual(title, b"a" * 1000)
8000 37+
38+
sum_info.SetProperty(msilib.PID_TITLE, "a" * 1001)
39+
title = sum_info.GetProperty(msilib.PID_TITLE)
40+
self.assertEqual(title, b"a" * 1001)
41+
finally:
42+
db = None
43+
sum_info = None
44+
unlink(db_path)
45+
646

747
class Test_make_id(unittest.TestCase):
848
#http://msdn.microsoft.com/en-us/library/aa369212(v=vs.85).aspx
@@ -35,12 +75,13 @@ def test_invalid_first_char(self):
3575
def test_invalid_any_char(self):
3676
self.assertEqual(
3777
msilib.make_id(".s\x82ort"), "_.s_ort")
38-
self.assertEqual (
78+
self.assertEqual(
3979
msilib.make_id(".s\x82o?*+rt"), "_.s_o___rt")
4080

4181

4282
def test_main():
4383
run_unittest(__name__)
4484

85+
4586
if __name__ == '__main__':
4687
test_main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Correctly handle string length in ``msilib.SummaryInfo.GetProperty()`` to
2+
prevent it from truncating the last character.

PC/_msi.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -539,14 +539,15 @@ summary_getproperty(msiobj* si, PyObject *args)
539539
FILETIME fval;
540540
char sbuf[1000];
541541
char *sval = sbuf;
542-
DWORD ssize = sizeof(sval);
542+
DWORD ssize = sizeof(sbuf);
543543

544544
if (!PyArg_ParseTuple(args, "i:GetProperty", &field))
545545
return NULL;
546546

547547
status = MsiSummaryInfoGetProperty(si->h, field, &type, &ival,
548548
&fval, sval, &ssize);
549549
if (status == ERROR_MORE_DATA) {
550+
ssize++;
550551
sval = malloc(ssize);
551552
if (sval == NULL) {
552553
return PyErr_NoMemory();
@@ -556,19 +557,29 @@ summary_getproperty(msiobj* si, PyObject *args)
556557
}
557558

558559
switch(type) {
559-
case VT_I2: case VT_I4:
560-
return PyInt_FromLong(ival);
560+
case VT_I2:
561+
case VT_I4:
562+
result = PyLong_FromLong(ival);
563+
break;
561564
case VT_FILETIME:
562565
PyErr_SetString(PyExc_NotImplementedError, "FILETIME result");
563-
return NULL;
566+
result = NULL;
567+
break;
564568
case VT_LPSTR:
565-
result = PyString_FromStringAndSize(sval, ssize);
566-
if (sval != sbuf)
567-
free(sval);
568-
return result;
569+
result = PyBytes_FromStringAndSize(sval, ssize);
570+
break;
571+
case VT_EMPTY:
572+
Py_INCREF(Py_None);
573+
result = Py_None;
574+
break;
575+
default:
576+
PyErr_Format(PyExc_NotImplementedError, "result of type %d", type);
577+
result = NULL;
578+
break;
569579
}
570-
PyErr_Format(PyExc_NotImplementedError, "result of type %d", type);
571-
return NULL;
580+
if (sval != sbuf)
581+
free(sval);
582+
return result;
572583
}
573584

574585
static PyObject*

0 commit comments

Comments
 (0)
0