@@ -299,7 +299,6 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
299299#ifdef CONFIG_OSDP_SC_ENABLED
300300 case CMD_KEYSET :
301301 if (len != CMD_KEYSET_DATA_LEN ) {
302- LOG_ERR ("CMD_KEYSET length mismatch! %d/18" , len );
303302 break ;
304303 }
305304 /**
@@ -342,7 +341,6 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
342341 break ;
343342 }
344343 if (len != CMD_CHLNG_DATA_LEN ) {
345- LOG_ERR ("CMD_CHLNG length mismatch! %d/8" , len );
346344 break ;
347345 }
348346 osdp_sc_init (pd );
@@ -356,7 +354,6 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
356354 }
357355 case CMD_SCRYPT :
358356 if (len != CMD_SCRYPT_DATA_LEN ) {
359- LOG_ERR ("CMD_SCRYPT length mismatch! %d/16" , len );
360357 break ;
361358 }
362359 for (i = 0 ; i < 16 ; i ++ ) {
@@ -373,11 +370,12 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
373370 return OSDP_PD_ERR_REPLY ;
374371 }
375372
376- if (ret != 0 ) {
377- LOG_ERR ("Invalid command structure. CMD: %02x, Len: %d" ,
378- pd -> cmd_id , len );
373+ if (ret == OSDP_PD_ERR_GENERIC ) {
374+ LOG_ERR ("Failed to decode command: CMD( %02x) Len:%d ret: %d" ,
375+ pd -> cmd_id , len , ret );
379376 pd -> reply_id = REPLY_NAK ;
380377 pd -> cmd_data [0 ] = OSDP_PD_NAK_CMD_LEN ;
378+ ret = OSDP_PD_ERR_REPLY ;
381379 }
382380
383381 if (pd -> cmd_id != CMD_POLL ) {
@@ -387,6 +385,12 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
387385 return ret ;
388386}
389387
388+ static inline void assert_buf_len (int need , int have )
389+ {
390+ __ASSERT (need < have , "OOM at build command: need:%d have:%d" ,
391+ need , have );
392+ }
393+
390394/**
391395 * Returns:
392396 * +ve: length of command
@@ -404,25 +408,15 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
404408#endif
405409 buf += data_off ;
406410 max_len -= data_off ;
407- if (max_len <= 0 ) {
408- LOG_ERR ("Out of buffer space!" );
409- return -1 ;
410- }
411411
412412 switch (pd -> reply_id ) {
413413 case REPLY_ACK :
414- if (max_len < REPLY_ACK_LEN ) {
415- LOG_ERR ("Out of buffer space!" );
416- break ;
417- }
414+ assert_buf_len (REPLY_ACK_LEN , max_len );
418415 buf [len ++ ] = pd -> reply_id ;
419416 ret = OSDP_PD_ERR_NONE ;
420417 break ;
421418 case REPLY_PDID :
422- if (max_len < REPLY_PDID_LEN ) {
423- LOG_ERR ("Out of buffer space!" );
424- break ;
425- }
419+ assert_buf_len (REPLY_PDID_LEN , max_len );
426420 buf [len ++ ] = pd -> reply_id ;
427421
428422 buf [len ++ ] = BYTE_0 (pd -> id .vendor_code );
@@ -443,10 +437,7 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
443437 ret = OSDP_PD_ERR_NONE ;
444438 break ;
445439 case REPLY_PDCAP :
446- if (max_len < REPLY_PDCAP_LEN ) {
447- LOG_ERR ("Out of buffer space!" );
448- break ;
449- }
440+ assert_buf_len (REPLY_PDCAP_LEN , max_len );
450441 buf [len ++ ] = pd -> reply_id ;
451442 for (i = 0 ; i < OSDP_PD_CAP_SENTINEL ; i ++ ) {
452443 if (pd -> cap [i ].function_code != i ) {
@@ -464,29 +455,20 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
464455 ret = OSDP_PD_ERR_NONE ;
465456 break ;
466457 case REPLY_LSTATR :
467- if (max_len < REPLY_LSTATR_LEN ) {
468- LOG_ERR ("Out of buffer space!" );
469- break ;
470- }
458+ assert_buf_len (REPLY_LSTATR_LEN , max_len );
471459 buf [len ++ ] = pd -> reply_id ;
472460 buf [len ++ ] = ISSET_FLAG (pd , PD_FLAG_TAMPER );
473461 buf [len ++ ] = ISSET_FLAG (pd , PD_FLAG_POWER );
474462 ret = OSDP_PD_ERR_NONE ;
475463 break ;
476464 case REPLY_RSTATR :
477- if (max_len < REPLY_RSTATR_LEN ) {
478- LOG_ERR ("Out of buffer space!" );
479- break ;
480- }
465+ assert_buf_len (REPLY_RSTATR_LEN , max_len );
481466 buf [len ++ ] = pd -> reply_id ;
482467 buf [len ++ ] = ISSET_FLAG (pd , PD_FLAG_R_TAMPER );
483468 ret = OSDP_PD_ERR_NONE ;
484469 break ;
485470 case REPLY_COM :
486- if (max_len < REPLY_COM_LEN ) {
487- LOG_ERR ("Out of buffer space!" );
488- break ;
489- }
471+ assert_buf_len (REPLY_COM_LEN , max_len );
490472 /**
491473 * If COMSET succeeds, the PD must reply with the old params and
492474 * then switch to the new params from then then on. We have the
@@ -517,10 +499,7 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
517499 ret = OSDP_PD_ERR_NONE ;
518500 break ;
519501 case REPLY_NAK :
520- if (max_len < REPLY_NAK_LEN ) {
521- LOG_ERR ("Fatal: insufficient space for sending NAK" );
522- return -1 ;
523- }
502+ assert_buf_len (REPLY_NAK_LEN , max_len );
524503 buf [len ++ ] = pd -> reply_id ;
525504 buf [len ++ ] = pd -> cmd_data [0 ];
526505 ret = OSDP_PD_ERR_NONE ;
@@ -530,10 +509,7 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
530509 if (smb == NULL ) {
531510 break ;
532511 }
533- if (max_len < REPLY_CCRYPT_LEN ) {
534- LOG_ERR ("Out of buffer space!" );
535- return -1 ;
536- }
512+ assert_buf_len (REPLY_CCRYPT_LEN , max_len );
537513 osdp_fill_random (pd -> sc .pd_random , 8 );
538514 osdp_compute_session_keys (pd );
539515 osdp_compute_pd_cryptogram (pd );
@@ -556,10 +532,7 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
556532 if (smb == NULL ) {
557533 break ;
558534 }
559- if (max_len < REPLY_RMAC_I_LEN ) {
560- LOG_ERR ("Out of buffer space!" );
561- return -1 ;
562- }
535+ assert_buf_len (REPLY_RMAC_I_LEN , max_len );
563536 osdp_compute_rmac_i (pd );
564537 buf [len ++ ] = pd -> reply_id ;
565538 for (i = 0 ; i < 16 ; i ++ ) {
@@ -595,10 +568,7 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
595568 /* catch all errors and report it as a RECORD error to CP */
596569 LOG_ERR ("Failed to build REPLY(%02x); Sending NAK instead!" ,
597570 pd -> reply_id );
598- if (max_len < REPLY_NAK_LEN ) {
599- LOG_ERR ("Fatal: insufficient space for sending NAK" );
600- return -1 ;
601- }
571+ assert_buf_len (REPLY_NAK_LEN , max_len );
602572 buf [0 ] = REPLY_NAK ;
603573 buf [1 ] = OSDP_PD_NAK_RECORD ;
604574 len = 2 ;
0 commit comments