Skip to contentSkip to conversation

staging: speakup: kobjects.c: fix char argument to %02x

PATCH In review Rasmus Villemoes 3 participants 5 messages Dec 6, 2015 – Dec 12, 2015 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: speakup: kobjects.c: fix char argument to %02x

If char is signed and ch happens to be negative, printing ch with "%02x" will not do as intended (when ch is -19, one will get "ffffffed"). Fix that by masking with 0xff.

---
 drivers/staging/speakup/kobjects.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/speakup/kobjects.c +1 -1
index fdfeb42b2b8f..8b88f03e266b 100644
@@ -567,7 +567,7 @@ ssize_t spk_var_show(struct kobject *kobj, struct kobj_attribute *attr,
 				if (ch >= ' ' && ch < '~')
 					*cp1++ = ch;
 				else
-					cp1 += sprintf(cp1, "\\x%02x", ch);
+					cp1 += sprintf(cp1, "\\x%02x", ch & 0xff);
 			}
 			*cp1++ = '"';
 			*cp1++ = '\n';
Signature · 1 line
2.6.1
#1 lore raw
Quoted 3 lines · Rasmus Villemoes

If char is signed and ch happens to be negative, printing ch with "%02x" will not do as intended (when ch is -19, one will get "ffffffed"). Fix that by masking with 0xff.

I presume there are a lot of these in the kernel. Did you use a tool to find this or just inspection?

#2 lore raw
Quoted 7 lines · Joe Perches
Quoted 3 lines · Rasmus Villemoes

If char is signed and ch happens to be negative, printing ch with "%02x" will not do as intended (when ch is -19, one will get "ffffffed"). Fix that by masking with 0xff.

I presume there are a lot of these in the kernel. Did you use a tool to find this or just inspection?

Initially I just used coccinelle, for the most obvious candidates (with --include-headers-for-types):

@r depends on !patch@
char c;
@@
* \( sprintf \| snprintf \| scnprintf \) (..., c, ...)

That gives lots of false positives (arguments to %c), but it's not too bad piping to less, searching for "%[0.]2[xX]", and then checking manually.

I'm now doing a much wider range of printf functions, but it's really past my bedtime, so feel free to pick up the ball :-)

Rasmus

#3 lore raw
Quoted 19 lines · Rasmus Villemoes
Quoted 7 lines · Joe Perches
Quoted 3 lines · Rasmus Villemoes

If char is signed and ch happens to be negative, printing ch with "%02x" will not do as intended (when ch is -19, one will get "ffffffed"). Fix that by masking with 0xff.

I presume there are a lot of these in the kernel. Did you use a tool to find this or just inspection?

Initially I just used coccinelle, for the most obvious candidates (with
--include-headers-for-types):

@r depends on !patch@
char c;
@@
* \( sprintf \| snprintf \| scnprintf \) (..., c, ...)

Quoted 8 lines

That gives lots of false positives (arguments to %c), but it's not too
bad piping to less, searching for "%[0.]2[xX]", and then checking manually.

I'm now doing a much wider range of printf functions, but it's really past my bedtime, so feel free to pick up the ball :-)

Thanks, but no thanks.  It's your ball.  Enjoy...

You might consider looking for s8 and s16 emitted as %u too.

#4 lore raw
In reply to Rasmus Villemoes
Quoted 14 lines · Rasmus Villemoes

If char is signed and ch happens to be negative, printing ch with "%02x" will not do as intended (when ch is -19, one will get "ffffffed"). Fix that by masking with 0xff.

---
 drivers/staging/speakup/kobjects.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/speakup/kobjects.c +0 -0
index fdfeb42b2b8f..8b88f03e266b 100644

drivers/staging/speakup/kobjects.c ssize_t spk_var_show(struct kobject *kobj, struct kobj_attribute *attr, · line 570

Quoted 5 lines
drivers/staging/speakup/kobjects.c +1 -1
@@ -567,7 +567,7 @@ ssize_t spk_var_show(struct kobject *kobj, struct kobj_attribute *attr,
                                if (ch >= ' ' && ch < '~')
                                        *cp1++ = ch;
                                else
-                                       cp1 += sprintf(cp1, "\\x%02x", ch);
+                                       cp1 += sprintf(cp1, "\\x%02x", ch & 0xff);

Have you considered to use string_escape_str()?

This one clearly looks like ESCAPE_HEX.

Quoted 11 lines
drivers/staging/speakup/kobjects.c +0 -1
                        }
                        *cp1++ = '"';
                        *cp1++ = '\n';
--

2.6.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 2 lines
With Best Regards,
Andy Shevchenko