@@ -299,7 +299,6 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
299
299
#ifdef CONFIG_OSDP_SC_ENABLED
300
300
case CMD_KEYSET :
301
301
if (len != CMD_KEYSET_DATA_LEN ) {
302
- LOG_ERR ("CMD_KEYSET length mismatch! %d/18" , len );
303
302
break ;
304
303
}
305
304
/**
@@ -342,7 +341,6 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
342
341
break ;
343
342
}
344
343
if (len != CMD_CHLNG_DATA_LEN ) {
345
- LOG_ERR ("CMD_CHLNG length mismatch! %d/8" , len );
346
344
break ;
347
345
}
348
346
osdp_sc_init (pd );
@@ -356,7 +354,6 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
356
354
}
357
355
case CMD_SCRYPT :
358
356
if (len != CMD_SCRYPT_DATA_LEN ) {
359
- LOG_ERR ("CMD_SCRYPT length mismatch! %d/16" , len );
360
357
break ;
361
358
}
362
359
for (i = 0 ; i < 16 ; i ++ ) {
@@ -373,11 +370,12 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
373
370
return OSDP_PD_ERR_REPLY ;
374
371
}
375
372
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 );
379
376
pd -> reply_id = REPLY_NAK ;
380
377
pd -> cmd_data [0 ] = OSDP_PD_NAK_CMD_LEN ;
378
+ ret = OSDP_PD_ERR_REPLY ;
381
379
}
382
380
383
381
if (pd -> cmd_id != CMD_POLL ) {
@@ -387,6 +385,12 @@ static int pd_decode_command(struct osdp_pd *pd, uint8_t *buf, int len)
387
385
return ret ;
388
386
}
389
387
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
+
390
394
/**
391
395
* Returns:
392
396
* +ve: length of command
@@ -404,25 +408,15 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
404
408
#endif
405
409
buf += data_off ;
406
410
max_len -= data_off ;
407
- if (max_len <= 0 ) {
408
- LOG_ERR ("Out of buffer space!" );
409
- return -1 ;
410
- }
411
411
412
412
switch (pd -> reply_id ) {
413
413
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 );
418
415
buf [len ++ ] = pd -> reply_id ;
419
416
ret = OSDP_PD_ERR_NONE ;
420
417
break ;
421
418
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 );
426
420
buf [len ++ ] = pd -> reply_id ;
427
421
428
422
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)
443
437
ret = OSDP_PD_ERR_NONE ;
444
438
break ;
445
439
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 );
450
441
buf [len ++ ] = pd -> reply_id ;
451
442
for (i = 0 ; i < OSDP_PD_CAP_SENTINEL ; i ++ ) {
452
443
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)
464
455
ret = OSDP_PD_ERR_NONE ;
465
456
break ;
466
457
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 );
471
459
buf [len ++ ] = pd -> reply_id ;
472
460
buf [len ++ ] = ISSET_FLAG (pd , PD_FLAG_TAMPER );
473
461
buf [len ++ ] = ISSET_FLAG (pd , PD_FLAG_POWER );
474
462
ret = OSDP_PD_ERR_NONE ;
475
463
break ;
476
464
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 );
481
466
buf [len ++ ] = pd -> reply_id ;
482
467
buf [len ++ ] = ISSET_FLAG (pd , PD_FLAG_R_TAMPER );
483
468
ret = OSDP_PD_ERR_NONE ;
484
469
break ;
485
470
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 );
490
472
/**
491
473
* If COMSET succeeds, the PD must reply with the old params and
492
474
* 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)
517
499
ret = OSDP_PD_ERR_NONE ;
518
500
break ;
519
501
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 );
524
503
buf [len ++ ] = pd -> reply_id ;
525
504
buf [len ++ ] = pd -> cmd_data [0 ];
526
505
ret = OSDP_PD_ERR_NONE ;
@@ -530,10 +509,7 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
530
509
if (smb == NULL ) {
531
510
break ;
532
511
}
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 );
537
513
osdp_fill_random (pd -> sc .pd_random , 8 );
538
514
osdp_compute_session_keys (pd );
539
515
osdp_compute_pd_cryptogram (pd );
@@ -556,10 +532,7 @@ static int pd_build_reply(struct osdp_pd *pd, uint8_t *buf, int max_len)
556
532
if (smb == NULL ) {
557
533
break ;
558
534
}
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 );
563
536
osdp_compute_rmac_i (pd );
564
537
buf [len ++ ] = pd -> reply_id ;
565
538
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)
595
568
/* catch all errors and report it as a RECORD error to CP */
596
569
LOG_ERR ("Failed to build REPLY(%02x); Sending NAK instead!" ,
597
570
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 );
602
572
buf [0 ] = REPLY_NAK ;
603
573
buf [1 ] = OSDP_PD_NAK_RECORD ;
604
574
len = 2 ;
0 commit comments