[openib-general] [PATCH] diags/saquery: fix node_desc.description as string usages

Muli Ben-Yehuda muli at il.ibm.com
Sat Oct 28 13:12:11 PDT 2006


On Sat, Oct 28, 2006 at 10:04:25PM +0200, Sasha Khapyorsky wrote:
> 
> node_desc.description buffer is received from the network and should
> not be NULL-terminated. In such cases using it as regular string in
> functions like strcmp() or printf() leads to segmentation faults.
> This patch fixes such usages.
> 
> Signed-off-by: Sasha Khapyorsky <sashak at voltaire.com>
> ---
>  diags/src/saquery.c |   22 ++++++++++++++++------
>  1 files changed, 16 insertions(+), 6 deletions(-)
> 
> diff --git a/diags/src/saquery.c b/diags/src/saquery.c
> index 5b4a85e..f5b23fd 100644
> --- a/diags/src/saquery.c
> +++ b/diags/src/saquery.c
> @@ -90,17 +90,21 @@ static void
>  print_node_desc(ib_node_record_t *node_record)
>  {
>  	ib_node_info_t *p_ni = &(node_record->node_info);
> +	ib_node_desc_t *p_nd = &(node_record->node_desc);
>  	if (p_ni->node_type == IB_NODE_TYPE_CA)
>  	{
> +		char desc[sizeof(p_nd->description) + 1];
> +		memcpy(desc, p_nd->description, sizeof(p_nd->description));
> +		desc[sizeof(desc) - 1] = '\0';

No need for the -1 here - desc is (sizeof(p_nd->description) + 1), so
the terminating NULL should be at index sizeof().

>  void
>  print_node_record(ib_node_record_t *node_record)
>  {
> +	char desc[sizeof(node_record->node_desc.description) + 1];
>  	ib_node_info_t *p_ni = NULL;
>  	p_ni = &(node_record->node_info);
>          
> @@ -117,6 +121,10 @@ print_node_record(ib_node_record_t *node
>  		break;
>          }
>  
> +	memcpy(desc, node_record->node_desc.description,
> +	       sizeof(node_record->node_desc.description));
> +	desc[sizeof(desc) - 1] = '\0';

Same thing.

Cheers,
Muli






More information about the general mailing list