L
lovecreatesbea...
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> */
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> */