Skip to content

Commit 50cf883

Browse files
authored
Merge pull request #11 from hepingood/dev
fix: fix string buffer bugs
2 parents 625256b + 692f71a commit 50cf883

File tree

12 files changed

+234
-90
lines changed

12 files changed

+234
-90
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ LibDriver AT24CXX is the full function driver of AT24CXX launched by LibDriver.
3939

4040
/project includes the common Linux and MCU development board sample code. All projects use the shell script to debug the driver and the detail instruction can be found in each project's README.md.
4141

42+
/misra includes the LibDriver MISRA code scanning results.
43+
4244
### Install
4345

4446
Reference /interface IIC platform independent template and finish your platform IIC driver.

README_de.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ LibDriver AT24CXX ist der Treiber mit vollem Funktionsumfang von AT24CXX, der vo
3838

3939
/project enthält den allgemeinen Beispielcode für Linux- und MCU-Entwicklungsboards. Alle Projekte verwenden das Shell-Skript, um den Treiber zu debuggen, und die detaillierten Anweisungen finden Sie in der README.md jedes Projekts.
4040

41+
/misra enthält die Ergebnisse des LibDriver MISRA Code Scans.
42+
4143
### Installieren
4244

4345
Verweisen Sie auf eine plattformunabhängige IIC-Schnittstellenvorlage und stellen Sie Ihren Plattform-IIC-Treiber fertig.

README_ja.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ LibDriver AT24CXXは、LibDriverによって起動されたAT24CXXの全機能
3939

4040
/ projectディレクトリには、一般的に使用されるLinuxおよびマイクロコントローラー開発ボードのプロジェクトサンプルが含まれています。 すべてのプロジェクトは、デバッグ方法としてシェルスクリプトを使用しています。詳細については、各プロジェクトのREADME.mdを参照してください。
4141

42+
/ misraはLibDriver misraコードスキャン結果を含む。
43+
4244
### インストール
4345

4446
/ interfaceディレクトリにあるプラットフォームに依存しないIICバステンプレートを参照して、指定したプラットフォームのIICバスドライバを完成させます。

README_ko.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ LibDriver AT24CXX는 LibDriver에서 출시한 AT24CXX의 전체 기능 드라
3939

4040
/project 디렉토리에는 일반적으로 사용되는 Linux 및 마이크로컨트롤러 개발 보드의 프로젝트 샘플이 포함되어 있습니다. 모든 프로젝트는 디버깅 방법으로 셸 스크립트를 사용하며, 자세한 내용은 각 프로젝트의 README.md를 참조하십시오.
4141

42+
/misra 에는 LibDriver misra 코드 검색 결과가 포함됩니다.
43+
4244
### 설치
4345

4446
/interface 디렉토리에서 플랫폼 독립적인 IIC버스 템플릿을 참조하여 지정된 플랫폼에 대한 IIC버스 드라이버를 완성하십시오.

README_zh-Hans.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ LibDriver AT24CXX是LibDriver推出的AT24CXX的全功能驱动,该驱动提
3939

4040
/project目录包含了常用Linux与单片机开发板的工程样例。所有工程均采用shell脚本作为调试方法,详细内容可参考每个工程里面的README.md。
4141

42+
/misra目录包含了LibDriver MISRA代码扫描结果。
43+
4244
### 安装
4345

4446
参考/interface目录下与平台无关的IIC总线模板,完成指定平台的IIC总线驱动。

README_zh-Hant.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ LibDriver AT24CXX是LibDriver推出的AT24CXX的全功能驅動,該驅動提
3939

4040
/project目錄包含了常用Linux與單片機開發板的工程樣例。所有工程均採用shell腳本作為調試方法,詳細內容可參考每個工程裡面的README.md。
4141

42+
/misra目錄包含了LibDriver MISRA程式碼掃描結果。
43+
4244
### 安裝
4345

4446
參考/interface目錄下與平台無關的IIC總線模板,完成指定平台的IIC總線驅動。

project/raspberrypi4b/driver/src/raspberrypi4b_driver_at24cxx_interface.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ void at24cxx_interface_delay_ms(uint32_t ms)
155155
void at24cxx_interface_debug_print(const char *const fmt, ...)
156156
{
157157
char str[256];
158-
uint8_t len;
158+
uint16_t len;
159159
va_list args;
160160

161161
memset((char *)str, 0, sizeof(char) * 256);
162162
va_start(args, fmt);
163-
vsnprintf((char *)str, 256, (char const *)fmt, args);
163+
vsnprintf((char *)str, 255, (char const *)fmt, args);
164164
va_end(args);
165165

166166
len = strlen((char *)str);

project/raspberrypi4b/src/main.c

Lines changed: 109 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,17 @@
3737
#include "driver_at24cxx_read_test.h"
3838
#include "driver_at24cxx_basic.h"
3939
#include <getopt.h>
40+
#include <math.h>
4041
#include <stdlib.h>
4142

4243
/**
4344
* @brief at24cxx full function
4445
* @param[in] argc is arg numbers
4546
* @param[in] **argv is the arg address
4647
* @return status code
47-
* - 0 success
48-
* - 1 run failed
49-
* - 5 param is invalid
48+
* - 0 success
49+
* - 1 run failed
50+
* - 5 param is invalid
5051
* @note none
5152
*/
5253
uint8_t at24cxx(uint8_t argc, char **argv)
@@ -67,7 +68,7 @@ uint8_t at24cxx(uint8_t argc, char **argv)
6768
{"type", required_argument, NULL, 4},
6869
{NULL, 0, NULL, 0},
6970
};
70-
char type[32] = "unknow";
71+
char type[33] = "unknow";
7172
uint8_t data = rand() % 0xFF;
7273
uint16_t reg = 0x0000;
7374
at24cxx_t chip_type = AT24C01;
@@ -96,7 +97,7 @@ uint8_t at24cxx(uint8_t argc, char **argv)
9697
case 'h' :
9798
{
9899
/* set the type */
99-
memset(type, 0, sizeof(char) * 32);
100+
memset(type, 0, sizeof(char) * 33);
100101
snprintf(type, 32, "h");
101102

102103
break;
@@ -106,7 +107,7 @@ uint8_t at24cxx(uint8_t argc, char **argv)
106107
case 'i' :
107108
{
108109
/* set the type */
109-
memset(type, 0, sizeof(char) * 32);
110+
memset(type, 0, sizeof(char) * 33);
110111
snprintf(type, 32, "i");
111112

112113
break;
@@ -116,7 +117,7 @@ uint8_t at24cxx(uint8_t argc, char **argv)
116117
case 'p' :
117118
{
118119
/* set the type */
119-
memset(type, 0, sizeof(char) * 32);
120+
memset(type, 0, sizeof(char) * 33);
120121
snprintf(type, 32, "p");
121122

122123
break;
@@ -126,7 +127,7 @@ uint8_t at24cxx(uint8_t argc, char **argv)
126127
case 'e' :
127128
{
128129
/* set the type */
129-
memset(type, 0, sizeof(char) * 32);
130+
memset(type, 0, sizeof(char) * 33);
130131
snprintf(type, 32, "e_%s", optarg);
131132

132133
break;
@@ -136,7 +137,7 @@ uint8_t at24cxx(uint8_t argc, char **argv)
136137
case 't' :
137138
{
138139
/* set the type */
139-
memset(type, 0, sizeof(char) * 32);
140+
memset(type, 0, sizeof(char) * 33);
140141
snprintf(type, 32, "t_%s", optarg);
141142

142143
break;
@@ -189,63 +190,128 @@ uint8_t at24cxx(uint8_t argc, char **argv)
189190
/* data */
190191
case 2 :
191192
{
193+
char *p;
194+
uint16_t l;
195+
uint16_t i;
196+
uint64_t hex_data;
197+
192198
/* set the data */
193-
if ((optarg[0] <= '9') && (optarg[0] >= '0'))
199+
l = strlen(optarg);
200+
201+
/* check the header */
202+
if (l >= 2)
194203
{
195-
data = (optarg[0] - '0') * 16;
204+
if (strncmp(optarg, "0x", 2) == 0)
205+
{
206+
p = optarg + 2;
207+
l -= 2;
208+
}
209+
else if (strncmp(optarg, "0X", 2) == 0)
210+
{
211+
p = optarg + 2;
212+
l -= 2;
213+
}
214+
else
215+
{
216+
p = optarg;
217+
}
196218
}
197219
else
198220
{
199-
data = (optarg[0] - 'A' + 10) * 16;
200-
}
201-
if ((optarg[1] <= '9') && (optarg[1] >= '0'))
202-
{
203-
data += (optarg[1] - '0');
221+
p = optarg;
204222
}
205-
else
223+
224+
/* init 0 */
225+
hex_data = 0;
226+
227+
/* loop */
228+
for (i = 0; i < l; i++)
206229
{
207-
data += (optarg[1] - 'A' + 10);
230+
if ((p[i] <= '9') && (p[i] >= '0'))
231+
{
232+
hex_data += (p[i] - '0') * (uint32_t)pow(16, l - i - 1);
233+
}
234+
else if ((p[i] <= 'F') && (p[i] >= 'A'))
235+
{
236+
hex_data += ((p[i] - 'A') + 10) * (uint32_t)pow(16, l - i - 1);
237+
}
238+
else if ((p[i] <= 'f') && (p[i] >= 'a'))
239+
{
240+
hex_data += ((p[i] - 'a') + 10) * (uint32_t)pow(16, l - i - 1);
241+
}
242+
else
243+
{
244+
return 5;
245+
}
208246
}
209247

248+
/* set the data */
249+
data = hex_data & 0xFF;
250+
210251
break;
211252
}
212253

213254
/* register */
214255
case 3 :
215256
{
216-
if ((optarg[0] <= '9') && (optarg[0] >= '0'))
217-
{
218-
reg = (optarg[0] - '0') * 16 * 16 *16;
219-
}
220-
else
221-
{
222-
reg = (optarg[0] - 'A' + 10) * 16 * 16 *16;
223-
}
224-
if ((optarg[1] <= '9') && (optarg[1] >= '0'))
225-
{
226-
reg += (optarg[1] - '0') * 16 * 16;
227-
}
228-
else
229-
{
230-
reg += (optarg[1] - 'A' + 10) * 16 * 16;
231-
}
232-
if ((optarg[2] <= '9') && (optarg[2] >= '0'))
257+
char *p;
258+
uint16_t l;
259+
uint16_t i;
260+
uint64_t hex_data;
261+
262+
/* set the data */
263+
l = strlen(optarg);
264+
265+
/* check the header */
266+
if (l >= 2)
233267
{
234-
reg += (optarg[2] - '0') * 16;
268+
if (strncmp(optarg, "0x", 2) == 0)
269+
{
270+
p = optarg + 2;
271+
l -= 2;
272+
}
273+
else if (strncmp(optarg, "0X", 2) == 0)
274+
{
275+
p = optarg + 2;
276+
l -= 2;
277+
}
278+
else
279+
{
280+
p = optarg;
281+
}
235282
}
236283
else
237284
{
238-
reg += (optarg[2] - 'A' + 10) * 16;
285+
p = optarg;
239286
}
240-
if ((optarg[3] <= '9') && (optarg[3] >= '0'))
241-
{
242-
reg += (optarg[3] - '0');
243-
}
244-
else
287+
288+
/* init 0 */
289+
hex_data = 0;
290+
291+
/* loop */
292+
for (i = 0; i < l; i++)
245293
{
246-
reg += (optarg[3] - 'A' + 10);
294+
if ((p[i] <= '9') && (p[i] >= '0'))
295+
{
296+
hex_data += (p[i] - '0') * (uint32_t)pow(16, l - i - 1);
297+
}
298+
else if ((p[i] <= 'F') && (p[i] >= 'A'))
299+
{
300+
hex_data += ((p[i] - 'A') + 10) * (uint32_t)pow(16, l - i - 1);
301+
}
302+
else if ((p[i] <= 'f') && (p[i] >= 'a'))
303+
{
304+
hex_data += ((p[i] - 'a') + 10) * (uint32_t)pow(16, l - i - 1);
305+
}
306+
else
307+
{
308+
return 5;
309+
}
247310
}
248311

312+
/* set the data */
313+
reg = hex_data & 0xFFFF;
314+
249315
break;
250316
}
251317

project/stm32f407/EW/stm32f407.dep

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<configuration>
66
<name>Release</name>
77
<outputs>
8-
<file>$PROJ_DIR$\..\usr\src\getopt.c</file>
9-
<file>$PROJ_DIR$\..\usr\src\stm32f4xx_it.c</file>
10-
<file>$PROJ_DIR$\..\usr\src\shell.c</file>
118
<file>$PROJ_DIR$\..\usr\src\main.c</file>
9+
<file>$PROJ_DIR$\..\usr\src\shell.c</file>
10+
<file>$PROJ_DIR$\..\usr\src\getopt.c</file>
1211
<file>$PROJ_DIR$\..\usr\src\stm32f4xx_hal_msp.c</file>
1312
<file>$PROJ_DIR$\..\usr\src\system_stm32f4xx.c</file>
13+
<file>$PROJ_DIR$\..\usr\src\stm32f4xx_it.c</file>
1414
</outputs>
1515
<forcedrebuild>
1616
<name>[REBUILD_ALL]</name>

project/stm32f407/driver/src/stm32f407_driver_at24cxx_interface.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ void at24cxx_interface_delay_ms(uint32_t ms)
147147
void at24cxx_interface_debug_print(const char *const fmt, ...)
148148
{
149149
char str[256];
150-
uint8_t len;
150+
uint16_t len;
151151
va_list args;
152152

153153
memset((char *)str, 0, sizeof(char) * 256);
154154
va_start(args, fmt);
155-
vsnprintf((char *)str, 256, (char const *)fmt, args);
155+
vsnprintf((char *)str, 255, (char const *)fmt, args);
156156
va_end(args);
157157

158158
len = strlen((char *)str);

project/stm32f407/interface/src/uart.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ uint16_t uart_flush(void)
203203
uint16_t uart_print(const char *const fmt, ...)
204204
{
205205
char str[256];
206-
uint8_t len;
206+
uint16_t len;
207207
va_list args;
208208

209209
/* print to the buffer */
210210
memset((char *)str, 0, sizeof(char) * 256);
211211
va_start(args, fmt);
212-
vsnprintf((char *)str, 256, (char const *)fmt, args);
212+
vsnprintf((char *)str, 255, (char const *)fmt, args);
213213
va_end(args);
214214

215215
/* send the data */

0 commit comments

Comments
 (0)