- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在努力确保我的编码遵循对象的正确处理,因此我将这些规则作为错误执行。但是我在处理这段代码时遇到了问题
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
class MyClass
{
public String ToXml()
{
var objSerializer =
new DataContractSerializer(GetType());
var objStream = new MemoryStream();
StreamReader objReader;
String strResult;
try
{
// Serialize the object
objSerializer.WriteObject(objStream, this);
// Move to start of stream to read out contents
objStream.Seek(0, SeekOrigin.Begin);
objReader = new StreamReader(objStream);
try
{
// Read Contents into a string
strResult = objReader.ReadToEnd();
}
finally
{
objReader.Dispose();
}
}
finally
{
if (objStream != null)
{
// objStream.Dispose();
}
}
return strResult;
}
}
如果我注释掉 objStream.Dispose()
,我会得到 CA2000,因为我没有处理对象,但如果我删除注释,它就会说我处理了不止一次。
还有什么正在处理这个对象?还是我在处理多个流时做错了?
最佳答案
这种情况现在也让我很烦。每隔几年,我决定通过运行 fxcop 或 Visual Studio 中现在内置的代码分析来刷新代码分析“规则”。
我最初写这段代码的时候,认为我是一个好公民,因为我正确地使用了 usings 来处理:
using (MemoryStream msDecrypt = new MemoryStream())
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
{
csDecrypt.Write(eop.m_Ciphertext, 0, eop.m_Ciphertext.Length);
}
decrypted = msDecrypt.ToArray();
}
此代码块导致 CA2202“不要多次处理对象” 关于此规则的最大讽刺是,它并不是真正与您的代码有关的问题,尽管它是保护您解决其他代码中的问题。 Microsoft 一直有大量关于应如何实现 Dispose 模式 (http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx) 的文档。然而,通过查看此代码分析规则 (http://msdn.microsoft.com/en-us/library/ms182334.aspx) 的详细信息,它揭示了此规则的目的
"A correctly implemented Dispose method can be called multiple times without throwing an exception. However, this is not guaranteed and to avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object."
简而言之,这条规则就是为了保护自己免受不遵守规则的人的伤害。
自然地,我将代码修改为如下所示:
MemoryStream msDecrypt = new MemoryStream()
//using (MemoryStream msDecrypt = new MemoryStream())
//{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
{
csDecrypt.Write(eop.m_Ciphertext, 0, eop.m_Ciphertext.Length);
}
decrypted = msDecrypt.ToArray();
//}
现在这个堆栈溢出帖子上的每个人都痛苦地意识到这个新问题,我们的 friend CA2000“在失去作用域之前处理对象” ...所以在这一点上我只是捂着脸一分钟。做了一些谷歌搜索,找到了这篇文章。就在那时我突然意识到,要通过这两个 CA 规则,您需要确保所有代码分支的所有内容都只处理一次。所以我开始着手做这件事,一旦你意识到这是你需要做的,这就不是一个难题。
自然地,代码演变成这样:
MemoryStream msDecrypt = null;
CryptoStream csDecrypt = null;
try
{
msDecrypt = new MemoryStream();
csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write);
csDecrypt.Write(eop.m_Ciphertext, 0, eop.m_Ciphertext.Length);
csDecrypt.FlushFinalBlock();
decrypted = msDecrypt.ToArray();
}
finally
{
if (csDecrypt != null)
{
csDecrypt.Dispose();
}
else if (msDecrypt != null)
{
msDecrypt.Dispose();
}
}
最后,我的代码没有生成 CA2000 或 CA2202。这个故事的寓意是,由于代码分析规则已经以这种方式发展,USING 语句的值(value)远不如过去。
您可以通过几种不同的方式编写代码来完成这项工作,我只是选择了一种不会将显式调用与 using 语句混合使用的方式,因为我相信这种方式更易于阅读和结构化以某种方式阻止某人只是去包装另一个使用它,从而在不知不觉中导致最初的问题。
关于c# - 代码分析规则 CA2000/CA 2202,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10139718/
我正在尝试在 Java 中执行此操作,但我认为这是一个一般证书问题。我有一个根CA,一个由根CA颁发的中间CA1,一个由中间CA1颁发的中间CA2,以及一个由中间CA2颁发的证书。 rootCA ->
编辑 1:https://security.stackexchange.com/questions/83972/trust-ca-and-parent-ca-but-not-other-derivat
我正在使用“任何?” block 中的方法。该片段正在字符串中查找字符串“CA”(拆分)检查: region="CA" check="AU,US,UK,CA,ZA" if check.split(',
我有一个SpringBoot应用程序,它使用以下配置与PostgreSQL通信,通过AWS Beanstrik部署:。在我将AWS Aurora证书更新为rds-ca-ecc384-g1之前,一切都很
我们正在使用我们现有的 CA 进行 freeipa 安装。在安装过程中,会生成 CSR,并且必须由 CA 签名才能创建证书。这个证书必须有 X509v3 Basic Constraints: CA:T
我正在尝试导出客户端证书以供网络浏览器使用。 目标是使用 指令限制对管理区域的访问。我看过很多关于使用自签名 CA 的教程。你会如何使用第三方来做到这一点? 1) 如果它是受信任的根 CA,我是否需要
我已经设法弄清楚 x509Certificate2Collection 中的证书是否是证书颁发机构证书,但我如何才能安全地确定它是根证书还是中间证书?以下是否足够安全? var collection
我使用 fabric-ca-sdk(fabric-sdk-java/fabric-sdk-java/src/test/fixture/sdkintegration) 中的测试代码启动 ca 服务器。并
环境: Red Hat Enterprise Linux Server release 7.7 (Maipo) # openssl version OpenSSL 1.0.2g 1 Mar 2016
导出 K8s 集群 CA 证书和 CA 私钥 团队,我有一个 Kubernetes 集群正在运行。我将一次又一次地删除和创建它,所以我想一直重复使用相同的 CA 证书,我需要保存 CA 证书和 key
我正在编写一个自定义客户端和服务器,我想通过公共(public) Internet 安全地进行通信,因此我想使用 OpenSSL 并让两端进行对等验证以确保我的客户端不会被 MITM 误导,同样,未经
问题: 我想构建一个 docker 容器 FROM:ubuntu:20.04但我无法访问外部互联网 我在内部网络上有一个 apt 镜像,可以使用 apt 镜像位于 https 后面,带有自定义证书 我
Linux 的新手,正在尝试了解更多,我遇到了这种情况。 我已经尝试使用 ps 命令并使用 grep 来捕获“ca”,但它会返回每次出现的“ca”,无论它来自什么,它实际上对我没有帮助。 我已经尝试过
我正在尝试在我的 .NET 应用程序和我安装了第三方根 CA 证书和中间 CA 证书的网站之间建立 TLS 连接: ServicePointManager.SecurityProtocol = Sec
SSL 证书永远不会让我眼花缭乱。我有一个网络应用程序,它从合作伙伴那里对另一项服务进行休息调用以获取某些数据。他们使用为公司生成的自签名或内部 CA。问题是每当另一端更新 SSL 证书时,我的应用程
我正在开发一个带有证书固定的移动应用程序。我将在 DMZ 中有一个盒子来代理我的请求。该服务器是否应该拥有来自可信 CA 的证书,还是我可以使用我自己的 CA 生成的证书? 从移动客户端使用受信任的
有没有人设法将 CA 证书安装到 activemq 实例中?我一直在进行谷歌搜索并阅读 activemq 文档,但我没有找到任何关于如何在 activemq 中使用预先存在的 CA 证书的信息。 我假
openssl ca 和 openssl x509 命令有什么区别?我正在使用它来创建和签署我的 root-ca、intermed-ca 和客户端证书,但是 openssl ca 命令不会在证书上注册
在 keystore 中创建私钥和自签名证书 keytool -genkey -alias mydomain -keystore mydomain.ks -dname cn=mydomain.com
我的 Raspberry Pi 3 出了点问题。我不得不运行 fsck.ext3,但是很多包都损坏了,例如 python 等。现在,ca-certificates 不会重新安装。每当它运行 updat
我是一名优秀的程序员,十分优秀!