Skip to contentSkip to conversation

strncmp uses in arch/m68k/sun3/prom/console.c

DISCUSSION Joe Perches 2 participants 2 messages Nov 9, 2009

lore
Share & tools
lore
#0 lore raw

These might be corrected to sizeof("foo") - 1

arch/m68k/sun3/prom/console.c:          if(strncmp(propb, "serial", sizeof("serial")))
arch/m68k/sun3/prom/console.c:                  strncmp("display", propb, sizeof("display")) == 0)
arch/m68k/sun3/prom/console.c:                  if(strncmp("serial", propb, sizeof("serial")))
#1 lore raw
Quoted 5 lines · Joe Perches

These might be corrected to sizeof("foo") - 1

arch/m68k/sun3/prom/console.c:          if(strncmp(propb, "serial", sizeof("serial")))
arch/m68k/sun3/prom/console.c:                  strncmp("display", propb, sizeof("display")) == 0)
arch/m68k/sun3/prom/console.c:                  if(strncmp("serial", propb, sizeof("serial")))

May I suggest:

	// Expands to a value equal to strlen( str ), but for string  
constants only,
	// and in constant (zero) time instead of linear time.
	#define STRLEN( str )  (sizeof "" str - 1)
	
	// Expands to two comma-separated values -- the string constant and  
its length.
	#define STR_LEN( str )  str, STRLEN( str )

	if ( strncmp( propb, STR_LEN( "serial" ) ) != 0 )

Also, since you know the length of one of the strings, you could safely call memcmp() instead.

Josh