Skip to content

Commit 48554a2

Browse files
author
Colin Robertson
committed
Fix longstanding bugs in code analysis walkthrough
1 parent f18e7e7 commit 48554a2

File tree

2 files changed

+97
-84
lines changed

2 files changed

+97
-84
lines changed

docs/code-quality/demo-sample.md

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Sample C++ project for code analysis
3-
ms.date: 11/04/2016
3+
description: "How to create a sample solution for use in the code analysis walkthrough for Microsoft C++ in Visual Studio."
4+
ms.date: 04/14/2020
45
ms.topic: sample
56
helpviewer_keywords:
67
- "demo sample [Visual Studio ALM]"
@@ -11,45 +12,45 @@ ms.assetid: 09e1b9f7-5916-4ed6-a001-5c2d7e710682
1112

1213
This following procedures show you how to create the sample for [Walkthrough: Analyze C/C++ code for defects](../code-quality/walkthrough-analyzing-c-cpp-code-for-defects.md). The procedures create:
1314

14-
- A Visual Studio solution named CppDemo.
15+
- A Visual Studio solution named *CppDemo*.
1516

16-
- A static library project named CodeDefects.
17+
- A static library project named *CodeDefects*.
1718

18-
- A static library project named Annotations.
19+
- A static library project named *Annotations*.
1920

2021
The procedures also provide the code for the header and *.cpp* files for the static libraries.
2122

2223
## Create the CppDemo solution and the CodeDefects project
2324

2425
1. Open Visual Studio and select **Create a new project**
2526

26-
1. Change language filter to **C++**
27+
1. In the **Create a new project** dialog, change the language filter to **C++**.
2728

28-
1. Select **Empty Project** and click **Next**
29+
1. Select **Windows Desktop Wizard** and choose the **Next** button.
2930

30-
1. In the **Project Name** text box, type **CodeDefects**
31+
1. On the **Configure your new project** page, in the **Project name** text box, enter **CodeDefects**.
3132

32-
1. In the **Solution name** text box, type **CppDemo**
33+
1. In the **Solution name** text box, enter *CppDemo*.
3334

34-
1. Click **Create**
35+
1. Choose **Create**.
3536

36-
## Configure the CodeDefects project as a static library
37+
1. In the **Windows Desktop Project** dialog, change the **Application type** to **Static Library (.lib)**.
3738

38-
1. In Solution Explorer, right-click **CodeDefects** and then click **Properties**.
39+
1. Under **Additional options**, select **Empty project**.
3940

40-
1. Expand **Configuration Properties** and then click **General**.
41+
1. Choose **OK** to create the solution and project.
4142

42-
1. In the **General** list, change **Configuration Type**, to **Static library (.lib)**.
43+
## Add the header and source file to the CodeDefects project
4344

44-
1. In the **Advanced** list, change **Target File Extension** to **.lib**
45+
1. In Solution Explorer, expand **CodeDefects**.
4546

46-
## Add the header and source file to the CodeDefects project
47+
1. Right-click to open the context menu for **Header Files**. Choose **Add** > **New Item**.
4748

48-
1. In Solution Explorer, expand **CodeDefects**, right-click **Header Files**, click **Add**, and then click **New Item**.
49+
1. In the **Add New Item** dialog box, select **Visual C++** > **Code**, and then select **Header File (.h)**.
4950

50-
1. In the **Add New Item** dialog box, click **Code**, and then click **Header File (.h)**.
51+
1. In the **Name** edit box, enter *Bug.h*, and then choose the **Add** button.
5152

52-
1. In the **Name** box, type **Bug.h** and then click **Add**.
53+
1. In the edit window for *Bug.h*, select and delete the contents.
5354

5455
1. Copy the following code and paste it into the *Bug.h* file in the editor.
5556

@@ -58,9 +59,8 @@ The procedures also provide the code for the header and *.cpp* files for the sta
5859

5960
#include <windows.h>
6061

61-
// These functions are consumed by the sample
62-
// but are not defined. This project cannot be linked!
63-
bool CheckDomain(LPCTSTR);
62+
// Function prototypes
63+
bool CheckDomain(wchar_t const *);
6464
HRESULT ReadUserAccount();
6565

6666
// These constants define the common sizes of the
@@ -69,24 +69,34 @@ The procedures also provide the code for the header and *.cpp* files for the sta
6969
const int ACCOUNT_DOMAIN_LEN = 128;
7070
```
7171

72-
1. In Solution Explorer, right-click **Source Files**, point to **New**, and then click **New Item**.
72+
1. In Solution Explorer, right-click to open the context menu for **Source Files**. Choose **Add** > **New Item**.
7373

74-
1. In the **Add New Item** dialog box, click **C++ File (.cpp)**
74+
1. In the **Add New Item** dialog box, select **C++ File (.cpp)**.
7575

76-
1. In the **Name** box, type **Bug.cpp** and then click **Add**.
76+
1. In the **Name** edit box, enter *Bug.cpp*, and then choose the **Add** button.
7777

7878
1. Copy the following code and paste it into the *Bug.cpp* file in the editor.
7979

8080
```cpp
8181
#include "Bug.h"
8282

8383
// the user account
84-
TCHAR g_userAccount[USER_ACCOUNT_LEN] = {};
84+
wchar_t g_userAccount[USER_ACCOUNT_LEN] = { L"domain\\user" };
8585
int len = 0;
8686

87+
bool CheckDomain(wchar_t const* domain)
88+
{
89+
return (wcsnlen_s(domain, USER_ACCOUNT_LEN) > 0);
90+
}
91+
92+
HRESULT ReadUserAccount()
93+
{
94+
return S_OK;
95+
}
96+
8797
bool ProcessDomain()
8898
{
89-
TCHAR* domain = new TCHAR[ACCOUNT_DOMAIN_LEN];
99+
wchar_t* domain = new wchar_t[ACCOUNT_DOMAIN_LEN];
90100
// ReadUserAccount gets a 'domain\user' input from
91101
//the user into the global 'g_userAccount'
92102
if (ReadUserAccount())
@@ -95,22 +105,22 @@ The procedures also provide the code for the header and *.cpp* files for the sta
95105
// character onto the 'domain' buffer
96106
for (len = 0; (len < ACCOUNT_DOMAIN_LEN) && (g_userAccount[len] != L'\0'); len++)
97107
{
98-
if (g_userAccount[len] == '\\')
108+
if (g_userAccount[len] == L'\\')
99109
{
100110
// Stops copying on the domain and user separator ('\')
101111
break;
102112
}
103113
domain[len] = g_userAccount[len];
104114
}
105-
if ((len = ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != '\\'))
115+
if ((len = ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != L'\\'))
106116
{
107117
// '\' was not found. Invalid domain\user string.
108118
delete[] domain;
109119
return false;
110120
}
111121
else
112122
{
113-
domain[len] = '\0';
123+
domain[len] = L'\0';
114124
}
115125
// Process domain string
116126
bool result = CheckDomain(domain);
@@ -133,31 +143,33 @@ The procedures also provide the code for the header and *.cpp* files for the sta
133143
}
134144
```
135145

136-
1. Click the **File** menu, and then click **Save All**.
146+
1. On the menu bar, choose **File** > **Save All**.
137147

138148
## Add the Annotations project and configure it as a static library
139149

140-
1. In Solution Explorer, click **CppDemo**, point to **Add**, and then click **New Project**.
141-
142-
1. In the **Add a new project** dialog box, Change language filter to **C++** and select **Empty Project** then click **Next**.
150+
1. In Solution Explorer, right-click **CppDemo** to open the context menu. Choose **Add** > **New Project**.
143151

144-
1. In the **Project name** text box, type **Annotations**, and then click **Create**.
152+
1. In the **Add a new project** dialog box, select **Windows Desktop Wizard**, and then choose the **Next** button.
145153

146-
1. In Solution Explorer, right-click **Annotations** and then click **Properties**.
154+
1. On the **Configure your new project** page, in the **Project name** text box, enter *Annotations*, and then choose **Create**.
147155

148-
1. Expand **Configuration Properties** and then click **General**.
156+
1. In the **Windows Desktop Project** dialog, change the **Application type** to **Static Library (.lib)**.
149157

150-
1. In the **General** list, change **Configuration Type**, to and then click **Static library (.lib)**.
158+
1. Under **Additional options**, select **Empty project**.
151159

152-
1. In the **Advanced** list, select the text in the column next to **Target File extension**, and then type **.lib**.
160+
1. Choose **OK** to create the project.
153161

154162
## Add the header file and source file to the Annotations project
155163

156-
1. In Solution Explorer, expand **Annotations**, right-click **Header Files**, click **Add**, and then click **New Item**.
164+
1. In Solution Explorer, expand **Annotations**.
165+
166+
1. Right-click to open the context menu for **Header Files**. Choose **Add** > **New Item**.
167+
168+
1. In the **Add New Item** dialog box, select **Visual C++** > **Code**, and then select **Header File (.h)**.
157169

158-
1. In the **Add New Item** dialog box, click **Header File (.h)**.
170+
1. In the **Name** edit box, enter *annotations.h*, and then choose the **Add** button.
159171

160-
1. In the **Name** box, type **annotations.h** and then click **Add**.
172+
1. In the edit window for *annotations.h*, select and delete the contents.
161173

162174
1. Copy the following code and paste it into the *annotations.h* file in the editor.
163175

@@ -176,16 +188,23 @@ The procedures also provide the code for the header and *.cpp* files for the sta
176188
_Ret_maybenull_ LinkedList* AllocateNode();
177189
```
178190

179-
1. In Solution Explorer, right-click **Source Files**, point to **New**, and then click **New Item**.
191+
1. In Solution Explorer, right-click to open the context menu for **Source Files**. Choose **Add** > **New Item**.
180192

181-
1. In the **Add New Item** dialog box, click **Code** and then click **C++ File (.cpp)**
193+
1. In the **Add New Item** dialog box, select **C++ File (.cpp)**.
182194

183-
1. In the **Name** box, type **annotations.cpp** and then click **Add**.
195+
1. In the **Name** edit box, enter *annotations.cpp*, and then choose the **Add** button.
184196

185197
1. Copy the following code and paste it into the *annotations.cpp* file in the editor.
186198

187199
```cpp
188200
#include "annotations.h"
201+
#include <malloc.h>
202+
203+
_Ret_maybenull_ LinkedList* AllocateNode()
204+
{
205+
LinkedList* result = static_cast<LinkedList*>(malloc(sizeof(LinkedList)));
206+
return result;
207+
}
189208

190209
LinkedList* AddTail(LinkedList* node, int value)
191210
{
@@ -205,6 +224,6 @@ The procedures also provide the code for the header and *.cpp* files for the sta
205224
}
206225
```
207226

208-
1. Click the **File** menu, and then click **Save All**.
227+
1. On the menu bar, choose **File** > **Save All**.
209228

210229
The solution is now complete and should build without errors.

0 commit comments

Comments
 (0)