检查代码中的所有错误? [英] Check all errors in code?

查看:71
本文介绍了检查代码中的所有错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否检查了代码中所有库调用的所有错误条件?


是否有必要检查所有错误?是否方便检查所有

错误(或如何使用大量非商业逻辑相关代码块使代码清晰可读)?

来自net-snmp库的以下代码调用atoi五次,但

不会检查。我们是否围绕

atoi编写一个包装器my_atoi_with_error_check()并在其他地方调用这个新的?这是一个好习惯(如果

,为什么ANSI atoi不这样做?)


/ *< quote file = .. /net-snmp-5.4.1.1/snmplib/vacm.c*/


286 char *

287 _vacm_parse_config_access_common(struct vacm_accessEntry

** aptr,char * line)

288 {

289 struct vacm_accessEntry access;

290 char * cPrefix =(char *) & access.contextPrefix;

291 char * gName =(char *)& access.groupName;

292 size_t len;

293

294 access.status = atoi(line);

295 line = skip_token(line);

296 access.storageType = atoi(line );

297 line = skip_token(line);

298 access.securityModel = atoi(line);

299 line = skip_token(line );

300 access.securityLevel = atoi(line);

301 line = skip_token(line);

302 access.contextMatch = atoi (线);

303 line = skip_token(line);

304 len = sizeof(access.groupName);

305 line = read_config_read_octet_string(line,( u_char **)

& gName,& len);

306 len = sizeof(access.contextPrefix);

307 line = read_config_read_octet_string(line,(u_char **)

& cPrefix,& len);

308

309 * aptr = vacm_getAccessEntry(access .groupName,

310 access.contextPrefix,

311 access.securityModel,

312 access.securityLevel);

313 if(!* aptr)

314 * aptr = vacm_createAccessEntry(access.groupName,

315 access.contextPrefix,

316 access。 securityModel,

317 access.securityLevel);

318 if(!* aptr)

319返回NULL;

320

321(* aptr) - > status = access.status;

322(* aptr) - > storageType = access.storageType;

323(* aptr) - > securityModel = access.securityModel;

324(* aptr) - > securityLevel = access.securityLevel;

325(* aptr) - > contextMatch = access.contextMatch;

326返回行;

327}


/ *< / quote * /

解决方案

lovecreatesbea ... @ gmail.com写道:


您是否检查了代码中所有库调用的所有错误条件?


是否有必要检查所有错误?是否方便检查所有

错误(或如何使用大量非商业逻辑相关代码块使代码清晰可读)?

来自net-snmp库的以下代码调用atoi五次,但

不会检查。我们是否围绕

atoi编写一个包装器my_atoi_with_error_check()并在其他地方调用这个新的?这是一个好习惯(如果

,为什么ANSI atoi不会这样做?)



没办法检查atoi的结果,这就是为什么

在大多数情况下使用strtol更好。


-

Ian Collins。


9月19日下午4:08,Ian Collins< ian-n ... @ hotmail.comwrote:
< blockquote class =post_quotes>
lovecreatesbea ... @ gmail.com写道:


你检查代码中所有库调用的所有错误条件吗? ?


是否有必要检查所有错误?检查所有

错误(或如何使用大量非商业逻辑相关代码块来清理和读取代码)是否方便?


net-snmp库中的以下代码调用atoi五次,但是

检查无。我们是否围绕

atoi编写一个包装器my_atoi_with_error_check()并在其他地方调用这个新的?这是一个好习惯(如果它是b $ b,为什么ANSI atoi不这样做)?



无法检查atoi的结果,这就是为什么

最好使用strtol的情况。



我不仅仅意味着检查函数调用的返回代码。


特别是,我在问整齐地做复杂的错误检查。


lovecreatesbea ... @ gmail.com说:


你呢检查代码中所有库调用的所有错误条件?



据说你永远不应该检查你不知道的情况

如何处理。但除此之外,检查一切。


是否有必要检查所有错误?



知道你的程序是否有效是否有用?如果没有,那么程序

本身可能不是那么有用。


net-snmp库中的以下代码调用atoi五次但是

检查没有。



随意解释你如何检查atoi。一旦你没有做到这一点,

你将能够解释代码被破坏的原因。

-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


Do you check all error conditions for all library calls in you code?

Is it necessary to check all errors? Is it convenient to check all
errors (or how to make code clean and readable with mass of non
business logic related code block)?

The following code from net-snmp library calls atoi five times but
checks none. Do we code a wrapper my_atoi_with_error_check() around
atoi and call this new one everywhere else? Is it a good practice (If
it is, why ANSI atoi doesn''t do it)?

/* <quote file=../net-snmp-5.4.1.1/snmplib/vacm.c*/

286 char *
287 _vacm_parse_config_access_common(struct vacm_accessEntry
**aptr, char *line)
288 {
289 struct vacm_accessEntry access;
290 char *cPrefix = (char *) &access.contextPrefix;
291 char *gName = (char *) &access.groupName;
292 size_t len;
293
294 access.status = atoi(line);
295 line = skip_token(line);
296 access.storageType = atoi(line);
297 line = skip_token(line);
298 access.securityModel = atoi(line);
299 line = skip_token(line);
300 access.securityLevel = atoi(line);
301 line = skip_token(line);
302 access.contextMatch = atoi(line);
303 line = skip_token(line);
304 len = sizeof(access.groupName);
305 line = read_config_read_octet_string(line, (u_char **)
&gName, &len);
306 len = sizeof(access.contextPrefix);
307 line = read_config_read_octet_string(line, (u_char **)
&cPrefix, &len);
308
309 *aptr = vacm_getAccessEntry(access.groupName,
310 access.contextPrefix,
311 access.securityModel,
312 access.securityLevel);
313 if (!*aptr)
314 *aptr = vacm_createAccessEntry(access.groupName,
315 access.contextPrefix,
316 access.securityModel,
317 access.securityLevel);
318 if (!*aptr)
319 return NULL;
320
321 (*aptr)->status = access.status;
322 (*aptr)->storageType = access.storageType;
323 (*aptr)->securityModel = access.securityModel;
324 (*aptr)->securityLevel = access.securityLevel;
325 (*aptr)->contextMatch = access.contextMatch;
326 return line;
327 }

/* </quote*/

解决方案

lovecreatesbea...@gmail.com wrote:

Do you check all error conditions for all library calls in you code?

Is it necessary to check all errors? Is it convenient to check all
errors (or how to make code clean and readable with mass of non
business logic related code block)?

The following code from net-snmp library calls atoi five times but
checks none. Do we code a wrapper my_atoi_with_error_check() around
atoi and call this new one everywhere else? Is it a good practice (If
it is, why ANSI atoi doesn''t do it)?

There is no way to check the result of atoi, that''s why it''s better to
use strtol in most situations.

--
Ian Collins.


On Sep 19, 4:08 pm, Ian Collins <ian-n...@hotmail.comwrote:

lovecreatesbea...@gmail.com wrote:

Do you check all error conditions for all library calls in you code?

Is it necessary to check all errors? Is it convenient to check all
errors (or how to make code clean and readable with mass of non
business logic related code block)?

The following code from net-snmp library calls atoi five times but
checks none. Do we code a wrapper my_atoi_with_error_check() around
atoi and call this new one everywhere else? Is it a good practice (If
it is, why ANSI atoi doesn''t do it)?


There is no way to check the result of atoi, that''s why it''s better to
use strtol in most situations.

I did not just mean check the return code from a function call.

Especially, I''m asking how to do complicated error check neatly.


lovecreatesbea...@gmail.com said:

Do you check all error conditions for all library calls in you code?

It has been said that you should never check for a condition you don''t know
how to handle. But apart from that, check everything.

Is it necessary to check all errors?

Is it useful to know whether your program worked? If not, then the program
itself is probably not all that useful.

The following code from net-snmp library calls atoi five times but
checks none.

Feel free to explain how you''d check atoi. Once you''ve failed to do that,
you will be in a position to explain why the code is broken.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


这篇关于检查代码中的所有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆