Skip to content

Commit 74005b5

Browse files
StefanVKvadz
authored andcommitted
Forbid passing temporaries to soci::use()
Previously code doing this compiled but resulted in undefined behaviour due to using variable memory after destroying it, so explicitly prevent it from compiling now to avoid run-time problems later. Document this incompatible change and update the change log for the next release. Closes SOCI#1070. Closes SOCI#1071.
1 parent 1c37a5f commit 74005b5

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

CHANGES

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
This file contains the history of changes in the SOCI library.
22

3-
Latest version is v4.0.3 released on 2022-02-14.
3+
Please read at least the following section when upgrading from
4+
a previous version of the library.
5+
6+
INCOMPATIBLE CHANGES
7+
====================
8+
9+
Changes in behaviour not resulting in compilation errors
10+
--------------------------------------------------------
11+
12+
Changes in behaviour which may result in build errors
13+
-----------------------------------------------------
14+
15+
- Temporary values cannot be passed to soci::use() any longer.
16+
Previously this would compile but result in undefined behaviour
17+
during run-time, but now this doesn't compile any longer. Please
18+
create a temporary variable holding the value if necessary (#1070).
19+
20+
21+
Other changes in v4.1.0:
22+
23+
24+
History of the changes in the previous versions:
425

526
---
6-
Version 4.0.3 differs from 4.0.2 in the following ways:
27+
Version 4.0.3 (released on 2022-02-14) differs from 4.0.2 in the following ways:
728

829
Changes affecting all or multiple backends:
930
- Fix opening sessions from pool (#891).

include/soci/use.h

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ struct use_container<T, no_indicator>
4646

4747
} // namespace details
4848

49+
// soci::use is deleted for rvalues because it will likely lead to subtle stack-use-after-scope bugs.
50+
template <typename T>
51+
details::use_container<T, details::no_indicator> use(T &&t, const std::string &name = std::string()) = delete;
52+
53+
template <typename T>
54+
details::use_container<const T, indicator> use(T &&t, indicator & ind, std::string const &name = std::string()) = delete;
55+
4956
template <typename T>
5057
details::use_container<T, details::no_indicator> use(T &t, const std::string &name = std::string())
5158
{ return details::use_container<T, details::no_indicator>(t, name); }

tests/oracle/test-oracle.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1028,8 +1028,8 @@ TEST_CASE("Oracle to_number with rowset", "[oracle][rowset][to_number]")
10281028
rs = (sql.prepare << "select to_number('123456789012345') from dual");
10291029
double d = rs.begin()->get<double>(0);
10301030
ASSERT_EQUAL_EXACT(d, 123456789012345);
1031-
1032-
rs = (sql.prepare << "select to_number(:t) from dual", use(3.14));
1031+
const double pi = 3.14;
1032+
rs = (sql.prepare << "select to_number(:t) from dual", use(pi));
10331033
d = rs.begin()->get<double>(0);
10341034
ASSERT_EQUAL_EXACT(d, 3.14);
10351035
}

0 commit comments

Comments
 (0)