0

clang/win: Fix most -Wwriteable-strings warnings.

Many win32 APIs take non-const string pointers. I checked that MSDN documents
them as _In_ and says that they are inputs, and then added const_cast<>s at
the calling sites. (In one test, I introduced a helper struct so that there
can be fewer casts.)

This wasn't just busywork, I found one function that we were handing string
literals where the documentation explicitly said that that's not valid
(filed http://crbug.com/396705).

I didn't change the DECLARE_REGISTRY_APPID_RESOURCEID() call in cloud_print;
it sounds like that'll fix itself when we update to the 2014 sdk:
http://connect.microsoft.com/VisualStudio/feedback/details/806376/atl-hindrances-to-adopting-new-strictstrings-conformance-option-in-vs2013

BUG=396705,82385
R=rnk@chromium.org, rsleevi@chromium.org, sergeyu@chromium.org, vitalybuka@chromium.org

Review URL: https://codereview.chromium.org/413763003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@285051 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
thakis@chromium.org
2014-07-23 22:04:53 +00:00
parent c8680d6ef7
commit 900634e0e6
9 changed files with 68 additions and 40 deletions

@ -47,7 +47,7 @@ bool DenyFilePermission(const FilePath& path, DWORD permission) {
change.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE; change.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
change.Trustee.TrusteeForm = TRUSTEE_IS_NAME; change.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
change.Trustee.TrusteeType = TRUSTEE_IS_USER; change.Trustee.TrusteeType = TRUSTEE_IS_USER;
change.Trustee.ptstrName = L"CURRENT_USER"; change.Trustee.ptstrName = const_cast<wchar_t*>(L"CURRENT_USER");
PACL new_dacl; PACL new_dacl;
if (SetEntriesInAcl(1, &change, old_dacl, &new_dacl) != ERROR_SUCCESS) { if (SetEntriesInAcl(1, &change, old_dacl, &new_dacl) != ERROR_SUCCESS) {

@ -236,7 +236,8 @@ TEST_F(PortMonitorTest, FlowTest) {
EXPECT_TRUE(monitor2->pfnEndDocPort != NULL); EXPECT_TRUE(monitor2->pfnEndDocPort != NULL);
// These functions should fail if we have not impersonated the user. // These functions should fail if we have not impersonated the user.
EXPECT_FALSE(monitor2->pfnStartDocPort(port_handle, L"", 0, 0, NULL)); EXPECT_FALSE(monitor2->pfnStartDocPort(
port_handle, const_cast<wchar_t*>(L""), 0, 0, NULL));
EXPECT_FALSE(monitor2->pfnWritePort(port_handle, EXPECT_FALSE(monitor2->pfnWritePort(port_handle,
buffer, buffer,
kBufferSize, kBufferSize,
@ -251,7 +252,8 @@ TEST_F(PortMonitorTest, FlowTest) {
// Now impersonate so we can test the success case. // Now impersonate so we can test the success case.
ASSERT_TRUE(ImpersonateSelf(SecurityImpersonation)); ASSERT_TRUE(ImpersonateSelf(SecurityImpersonation));
EXPECT_TRUE(monitor2->pfnStartDocPort(port_handle, L"", 0, 0, NULL)); EXPECT_TRUE(monitor2->pfnStartDocPort(
port_handle, const_cast<wchar_t*>(L""), 0, 0, NULL));
EXPECT_TRUE(monitor2->pfnWritePort(port_handle, EXPECT_TRUE(monitor2->pfnWritePort(port_handle,
buffer, buffer,
kBufferSize, kBufferSize,

@ -36,7 +36,8 @@ bool GetSubjectPublicKeyInfo(HCRYPTPROV prov, std::vector<BYTE>* output) {
// as a CERT_PUBLIC_KEY_INFO structure. Currently, only RSA public keys are // as a CERT_PUBLIC_KEY_INFO structure. Currently, only RSA public keys are
// supported. // supported.
ok = CryptExportPublicKeyInfoEx(prov, AT_KEYEXCHANGE, X509_ASN_ENCODING, ok = CryptExportPublicKeyInfoEx(prov, AT_KEYEXCHANGE, X509_ASN_ENCODING,
szOID_RSA_RSA, 0, NULL, NULL, &size); const_cast<char*>(szOID_RSA_RSA), 0, NULL,
NULL, &size);
DCHECK(ok); DCHECK(ok);
if (!ok) if (!ok)
return false; return false;
@ -46,8 +47,8 @@ bool GetSubjectPublicKeyInfo(HCRYPTPROV prov, std::vector<BYTE>* output) {
PCERT_PUBLIC_KEY_INFO public_key_casted = PCERT_PUBLIC_KEY_INFO public_key_casted =
reinterpret_cast<PCERT_PUBLIC_KEY_INFO>(&(*output)[0]); reinterpret_cast<PCERT_PUBLIC_KEY_INFO>(&(*output)[0]);
ok = CryptExportPublicKeyInfoEx(prov, AT_KEYEXCHANGE, X509_ASN_ENCODING, ok = CryptExportPublicKeyInfoEx(prov, AT_KEYEXCHANGE, X509_ASN_ENCODING,
szOID_RSA_RSA, 0, NULL, public_key_casted, const_cast<char*>(szOID_RSA_RSA), 0, NULL,
&size); public_key_casted, &size);
DCHECK(ok); DCHECK(ok);
if (!ok) if (!ok)
return false; return false;
@ -82,7 +83,7 @@ bool GetSignedPublicKeyAndChallenge(HCRYPTPROV prov,
CRYPT_ALGORITHM_IDENTIFIER sig_alg; CRYPT_ALGORITHM_IDENTIFIER sig_alg;
memset(&sig_alg, 0, sizeof(sig_alg)); memset(&sig_alg, 0, sizeof(sig_alg));
sig_alg.pszObjId = szOID_RSA_MD5RSA; sig_alg.pszObjId = const_cast<char*>(szOID_RSA_MD5RSA);
BOOL ok; BOOL ok;
DWORD size = 0; DWORD size = 0;

@ -571,7 +571,7 @@ int CertVerifyProcWin::VerifyInternal(
// We still need to request szOID_SERVER_GATED_CRYPTO and szOID_SGC_NETSCAPE // We still need to request szOID_SERVER_GATED_CRYPTO and szOID_SGC_NETSCAPE
// today because some certificate chains need them. IE also requests these // today because some certificate chains need them. IE also requests these
// two usages. // two usages.
static const LPSTR usage[] = { static const LPCSTR usage[] = {
szOID_PKIX_KP_SERVER_AUTH, szOID_PKIX_KP_SERVER_AUTH,
szOID_SERVER_GATED_CRYPTO, szOID_SERVER_GATED_CRYPTO,
szOID_SGC_NETSCAPE szOID_SGC_NETSCAPE

@ -12,9 +12,16 @@
namespace net { namespace net {
TEST(ProxyConfigServiceWinTest, SetFromIEConfig) { TEST(ProxyConfigServiceWinTest, SetFromIEConfig) {
// Like WINHTTP_CURRENT_USER_IE_PROXY_CONFIG, but with const strings.
struct IEProxyConfig {
BOOL auto_detect;
const wchar_t* auto_config_url;
const wchar_t* proxy;
const wchar_t* proxy_bypass;
};
const struct { const struct {
// Input. // Input.
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config; IEProxyConfig ie_config;
// Expected outputs (fields of the ProxyConfig). // Expected outputs (fields of the ProxyConfig).
bool auto_detect; bool auto_detect;
@ -190,8 +197,13 @@ TEST(ProxyConfigServiceWinTest, SetFromIEConfig) {
}; };
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ie_config = {
tests[i].ie_config.auto_detect,
const_cast<wchar_t*>(tests[i].ie_config.auto_config_url),
const_cast<wchar_t*>(tests[i].ie_config.proxy),
const_cast<wchar_t*>(tests[i].ie_config.proxy_bypass)};
ProxyConfig config; ProxyConfig config;
ProxyConfigServiceWin::SetFromIEConfig(&config, tests[i].ie_config); ProxyConfigServiceWin::SetFromIEConfig(&config, ie_config);
EXPECT_EQ(tests[i].auto_detect, config.auto_detect()); EXPECT_EQ(tests[i].auto_detect, config.auto_detect());
EXPECT_EQ(tests[i].pac_url, config.pac_url()); EXPECT_EQ(tests[i].pac_url, config.pac_url());

@ -472,14 +472,18 @@ scoped_ptr<DEVMODE, base::FreeDeleter> CreateDevModeWithColor(
scoped_ptr<DEVMODE, base::FreeDeleter> CreateDevMode(HANDLE printer, scoped_ptr<DEVMODE, base::FreeDeleter> CreateDevMode(HANDLE printer,
DEVMODE* in) { DEVMODE* in) {
LONG buffer_size = DocumentProperties(NULL, printer, L"", NULL, NULL, 0); LONG buffer_size = DocumentProperties(
NULL, printer, const_cast<wchar_t*>(L""), NULL, NULL, 0);
if (buffer_size < static_cast<int>(sizeof(DEVMODE))) if (buffer_size < static_cast<int>(sizeof(DEVMODE)))
return scoped_ptr<DEVMODE, base::FreeDeleter>(); return scoped_ptr<DEVMODE, base::FreeDeleter>();
scoped_ptr<DEVMODE, base::FreeDeleter> out( scoped_ptr<DEVMODE, base::FreeDeleter> out(
reinterpret_cast<DEVMODE*>(malloc(buffer_size))); reinterpret_cast<DEVMODE*>(malloc(buffer_size)));
DWORD flags = (in ? (DM_IN_BUFFER) : 0) | DM_OUT_BUFFER; DWORD flags = (in ? (DM_IN_BUFFER) : 0) | DM_OUT_BUFFER;
if (DocumentProperties(NULL, printer, L"", out.get(), in, flags) != IDOK) if (DocumentProperties(
NULL, printer, const_cast<wchar_t*>(L""), out.get(), in, flags) !=
IDOK) {
return scoped_ptr<DEVMODE, base::FreeDeleter>(); return scoped_ptr<DEVMODE, base::FreeDeleter>();
}
CHECK_GE(buffer_size, out.get()->dmSize + out.get()->dmDriverExtra); CHECK_GE(buffer_size, out.get()->dmSize + out.get()->dmDriverExtra);
return out.Pass(); return out.Pass();
} }

@ -117,13 +117,14 @@ COMPILE_ASSERT(sizeof(ScopedVariantArg) == sizeof(VARIANTARG),
// - VARIANT is the only supported parameter type at the moment. // - VARIANT is the only supported parameter type at the moment.
HRESULT Invoke(IDispatch* object, HRESULT Invoke(IDispatch* object,
LPOLESTR name, LPCOLESTR const_name,
WORD flags, WORD flags,
VARIANT* const & result_out) { VARIANT* const & result_out) {
// Retrieve the ID of the method to be called. // Retrieve the ID of the method to be called.
DISPID disp_id; DISPID disp_id;
HRESULT hr = object->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, LPOLESTR name = const_cast<LPOLESTR>(const_name);
&disp_id); HRESULT hr = object->GetIDsOfNames(
IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &disp_id);
if (FAILED(hr)) if (FAILED(hr))
return hr; return hr;
@ -161,14 +162,15 @@ HRESULT Invoke(IDispatch* object,
template <typename P1> template <typename P1>
HRESULT Invoke(IDispatch* object, HRESULT Invoke(IDispatch* object,
LPOLESTR name, LPCOLESTR const_name,
WORD flags, WORD flags,
const P1& p1, const P1& p1,
VARIANT* const & result_out) { VARIANT* const & result_out) {
// Retrieve the ID of the method to be called. // Retrieve the ID of the method to be called.
DISPID disp_id; DISPID disp_id;
HRESULT hr = object->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, LPOLESTR name = const_cast<LPOLESTR>(const_name);
&disp_id); HRESULT hr = object->GetIDsOfNames(
IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &disp_id);
if (FAILED(hr)) if (FAILED(hr))
return hr; return hr;
@ -213,15 +215,16 @@ HRESULT Invoke(IDispatch* object,
template <typename P1, typename P2> template <typename P1, typename P2>
HRESULT Invoke(IDispatch* object, HRESULT Invoke(IDispatch* object,
LPOLESTR name, LPCOLESTR const_name,
WORD flags, WORD flags,
const P1& p1, const P1& p1,
const P2& p2, const P2& p2,
VARIANT* const & result_out) { VARIANT* const & result_out) {
// Retrieve the ID of the method to be called. // Retrieve the ID of the method to be called.
DISPID disp_id; DISPID disp_id;
HRESULT hr = object->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, LPOLESTR name = const_cast<LPOLESTR>(const_name);
&disp_id); HRESULT hr = object->GetIDsOfNames(
IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &disp_id);
if (FAILED(hr)) if (FAILED(hr))
return hr; return hr;
@ -270,7 +273,7 @@ HRESULT Invoke(IDispatch* object,
template <typename P1, typename P2, typename P3> template <typename P1, typename P2, typename P3>
HRESULT Invoke(IDispatch* object, HRESULT Invoke(IDispatch* object,
LPOLESTR name, LPCOLESTR const_name,
WORD flags, WORD flags,
const P1& p1, const P1& p1,
const P2& p2, const P2& p2,
@ -278,8 +281,9 @@ HRESULT Invoke(IDispatch* object,
VARIANT* const & result_out) { VARIANT* const & result_out) {
// Retrieve the ID of the method to be called. // Retrieve the ID of the method to be called.
DISPID disp_id; DISPID disp_id;
HRESULT hr = object->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, LPOLESTR name = const_cast<LPOLESTR>(const_name);
&disp_id); HRESULT hr = object->GetIDsOfNames(
IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &disp_id);
if (FAILED(hr)) if (FAILED(hr))
return hr; return hr;
@ -332,7 +336,7 @@ HRESULT Invoke(IDispatch* object,
template <typename P1, typename P2, typename P3, typename P4> template <typename P1, typename P2, typename P3, typename P4>
HRESULT Invoke(IDispatch* object, HRESULT Invoke(IDispatch* object,
LPOLESTR name, LPCOLESTR const_name,
WORD flags, WORD flags,
const P1& p1, const P1& p1,
const P2& p2, const P2& p2,
@ -341,8 +345,9 @@ HRESULT Invoke(IDispatch* object,
VARIANT* const & result_out) { VARIANT* const & result_out) {
// Retrieve the ID of the method to be called. // Retrieve the ID of the method to be called.
DISPID disp_id; DISPID disp_id;
HRESULT hr = object->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, LPOLESTR name = const_cast<LPOLESTR>(const_name);
&disp_id); HRESULT hr = object->GetIDsOfNames(
IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &disp_id);
if (FAILED(hr)) if (FAILED(hr))
return hr; return hr;
@ -399,7 +404,7 @@ HRESULT Invoke(IDispatch* object,
template <typename P1, typename P2, typename P3, typename P4, typename P5> template <typename P1, typename P2, typename P3, typename P4, typename P5>
HRESULT Invoke(IDispatch* object, HRESULT Invoke(IDispatch* object,
LPOLESTR name, LPCOLESTR const_name,
WORD flags, WORD flags,
const P1& p1, const P1& p1,
const P2& p2, const P2& p2,
@ -409,8 +414,9 @@ HRESULT Invoke(IDispatch* object,
VARIANT* const & result_out) { VARIANT* const & result_out) {
// Retrieve the ID of the method to be called. // Retrieve the ID of the method to be called.
DISPID disp_id; DISPID disp_id;
HRESULT hr = object->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, LPOLESTR name = const_cast<LPOLESTR>(const_name);
&disp_id); HRESULT hr = object->GetIDsOfNames(
IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &disp_id);
if (FAILED(hr)) if (FAILED(hr))
return hr; return hr;
@ -472,7 +478,7 @@ HRESULT Invoke(IDispatch* object,
template <typename P1, typename P2, typename P3, typename P4, typename P5, template <typename P1, typename P2, typename P3, typename P4, typename P5,
typename P6> typename P6>
HRESULT Invoke(IDispatch* object, HRESULT Invoke(IDispatch* object,
LPOLESTR name, LPCOLESTR const_name,
WORD flags, WORD flags,
const P1& p1, const P1& p1,
const P2& p2, const P2& p2,
@ -483,8 +489,9 @@ HRESULT Invoke(IDispatch* object,
VARIANT* const & result_out) { VARIANT* const & result_out) {
// Retrieve the ID of the method to be called. // Retrieve the ID of the method to be called.
DISPID disp_id; DISPID disp_id;
HRESULT hr = object->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, LPOLESTR name = const_cast<LPOLESTR>(const_name);
&disp_id); HRESULT hr = object->GetIDsOfNames(
IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &disp_id);
if (FAILED(hr)) if (FAILED(hr))
return hr; return hr;
@ -550,7 +557,7 @@ HRESULT Invoke(IDispatch* object,
template <typename P1, typename P2, typename P3, typename P4, typename P5, template <typename P1, typename P2, typename P3, typename P4, typename P5,
typename P6, typename P7> typename P6, typename P7>
HRESULT Invoke(IDispatch* object, HRESULT Invoke(IDispatch* object,
LPOLESTR name, LPCOLESTR const_name,
WORD flags, WORD flags,
const P1& p1, const P1& p1,
const P2& p2, const P2& p2,
@ -562,8 +569,9 @@ HRESULT Invoke(IDispatch* object,
VARIANT* const & result_out) { VARIANT* const & result_out) {
// Retrieve the ID of the method to be called. // Retrieve the ID of the method to be called.
DISPID disp_id; DISPID disp_id;
HRESULT hr = object->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, LPOLESTR name = const_cast<LPOLESTR>(const_name);
&disp_id); HRESULT hr = object->GetIDsOfNames(
IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &disp_id);
if (FAILED(hr)) if (FAILED(hr))
return hr; return hr;

@ -128,7 +128,7 @@ $range ARG 1..ARITY
$if ARITY > 0 [[template <$for ARG , [[typename P$(ARG)]]>]] $if ARITY > 0 [[template <$for ARG , [[typename P$(ARG)]]>]]
HRESULT Invoke(IDispatch* object, HRESULT Invoke(IDispatch* object,
LPOLESTR name, LPCOLESTR const_name,
WORD flags, WORD flags,
$for ARG [[ $for ARG [[
@ -138,8 +138,9 @@ $for ARG [[
VARIANT* const & result_out) { VARIANT* const & result_out) {
// Retrieve the ID of the method to be called. // Retrieve the ID of the method to be called.
DISPID disp_id; DISPID disp_id;
HRESULT hr = object->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, LPOLESTR name = const_cast<LPOLESTR>(const_name);
&disp_id); HRESULT hr = object->GetIDsOfNames(
IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &disp_id);
if (FAILED(hr)) if (FAILED(hr))
return hr; return hr;

@ -153,7 +153,7 @@ bool CreateMachineState() {
ea.grfAccessMode = GRANT_ACCESS; ea.grfAccessMode = GRANT_ACCESS;
ea.grfInheritance= SUB_CONTAINERS_AND_OBJECTS_INHERIT; ea.grfInheritance= SUB_CONTAINERS_AND_OBJECTS_INHERIT;
ea.Trustee.TrusteeForm = TRUSTEE_IS_NAME; ea.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
ea.Trustee.ptstrName = L"Everyone"; ea.Trustee.ptstrName = const_cast<wchar_t*>(L"Everyone");
ACL* new_dacl = NULL; ACL* new_dacl = NULL;
result = SetEntriesInAcl(1, &ea, dacl, &new_dacl); result = SetEntriesInAcl(1, &ea, dacl, &new_dacl);