[PATCH] drivers/media/pci: use memmove for overlapping regions

2013-01-07 Thread Nickolai Zeldovich
Change several memcpy() to memmove() in cases when the regions are
definitely overlapping; memcpy() of overlapping regions is undefined
behavior in C and can produce different results depending on the compiler,
the memcpy implementation, etc.

Signed-off-by: Nickolai Zeldovich 
---
 drivers/media/pci/bt8xx/dst_ca.c  |4 ++--
 drivers/media/pci/cx18/cx18-vbi.c |2 +-
 drivers/media/pci/ivtv/ivtv-vbi.c |4 ++--
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/media/pci/bt8xx/dst_ca.c b/drivers/media/pci/bt8xx/dst_ca.c
index 7d96fab..0e788fc 100644
--- a/drivers/media/pci/bt8xx/dst_ca.c
+++ b/drivers/media/pci/bt8xx/dst_ca.c
@@ -180,11 +180,11 @@ static int ca_get_app_info(struct dst_state *state)
put_command_and_length(&state->messages[0], CA_APP_INFO, length);
 
// Copy application_type, application_manufacturer and manufacturer_code
-   memcpy(&state->messages[4], &state->messages[7], 5);
+   memmove(&state->messages[4], &state->messages[7], 5);
 
// Set string length and copy string
state->messages[9] = str_length;
-   memcpy(&state->messages[10], &state->messages[12], str_length);
+   memmove(&state->messages[10], &state->messages[12], str_length);
 
return 0;
 }
diff --git a/drivers/media/pci/cx18/cx18-vbi.c 
b/drivers/media/pci/cx18/cx18-vbi.c
index 6d3121f..add9964 100644
--- a/drivers/media/pci/cx18/cx18-vbi.c
+++ b/drivers/media/pci/cx18/cx18-vbi.c
@@ -84,7 +84,7 @@ static void copy_vbi_data(struct cx18 *cx, int lines, u32 
pts_stamp)
   (the max size of the VBI data is 36 * 43 + 4 bytes).
   So in this case we use the magic number 'ITV0'. */
memcpy(dst + sd, "ITV0", 4);
-   memcpy(dst + sd + 4, dst + sd + 12, line * 43);
+   memmove(dst + sd + 4, dst + sd + 12, line * 43);
size = 4 + ((43 * line + 3) & ~3);
} else {
memcpy(dst + sd, "itv0", 4);
diff --git a/drivers/media/pci/ivtv/ivtv-vbi.c 
b/drivers/media/pci/ivtv/ivtv-vbi.c
index 293db80..3c156bc 100644
--- a/drivers/media/pci/ivtv/ivtv-vbi.c
+++ b/drivers/media/pci/ivtv/ivtv-vbi.c
@@ -224,7 +224,7 @@ static void copy_vbi_data(struct ivtv *itv, int lines, u32 
pts_stamp)
   (the max size of the VBI data is 36 * 43 + 4 bytes).
   So in this case we use the magic number 'ITV0'. */
memcpy(dst + sd, "ITV0", 4);
-   memcpy(dst + sd + 4, dst + sd + 12, line * 43);
+   memmove(dst + sd + 4, dst + sd + 12, line * 43);
size = 4 + ((43 * line + 3) & ~3);
} else {
memcpy(dst + sd, "itv0", 4);
@@ -532,7 +532,7 @@ void ivtv_vbi_work_handler(struct ivtv *itv)
while (vi->cc_payload_idx) {
cc = vi->cc_payload[0];
 
-   memcpy(vi->cc_payload, vi->cc_payload + 1,
+   memmove(vi->cc_payload, vi->cc_payload + 1,
sizeof(vi->cc_payload) - 
sizeof(vi->cc_payload[0]));
vi->cc_payload_idx--;
if (vi->cc_payload_idx && cc.odd[0] == 0x80 && 
cc.odd[1] == 0x80)
-- 
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2] media: cx18, ivtv: eliminate unnecessary array index checks

2013-01-06 Thread Nickolai Zeldovich
The idx values passed to cx18_i2c_register() and ivtv_i2c_register()
by cx18_init_subdevs() and ivtv_load_and_init_modules() respectively
are always in-range, based on how the hw_all bitmask is populated.
Previously, the checks were already ineffective because arrays were
being dereferenced using the index before the check.

Signed-off-by: Nickolai Zeldovich 
---
Thanks to Andy Walls for suggesting that instead of moving the checks
before array dereference, a better fix is to remove the checks altogether,
since they are superfluous.

 drivers/media/pci/cx18/cx18-i2c.c |3 ---
 drivers/media/pci/ivtv/ivtv-i2c.c |2 --
 2 files changed, 5 deletions(-)

diff --git a/drivers/media/pci/cx18/cx18-i2c.c 
b/drivers/media/pci/cx18/cx18-i2c.c
index 4908eb7..ccb1d15 100644
--- a/drivers/media/pci/cx18/cx18-i2c.c
+++ b/drivers/media/pci/cx18/cx18-i2c.c
@@ -116,9 +116,6 @@ int cx18_i2c_register(struct cx18 *cx, unsigned idx)
const char *type = hw_devicenames[idx];
u32 hw = 1 << idx;
 
-   if (idx >= ARRAY_SIZE(hw_addrs))
-   return -1;
-
if (hw == CX18_HW_TUNER) {
/* special tuner group handling */
sd = v4l2_i2c_new_subdev(&cx->v4l2_dev,
diff --git a/drivers/media/pci/ivtv/ivtv-i2c.c 
b/drivers/media/pci/ivtv/ivtv-i2c.c
index 46e262b..bc984af 100644
--- a/drivers/media/pci/ivtv/ivtv-i2c.c
+++ b/drivers/media/pci/ivtv/ivtv-i2c.c
@@ -267,8 +267,6 @@ int ivtv_i2c_register(struct ivtv *itv, unsigned idx)
const char *type = hw_devicenames[idx];
u32 hw = 1 << idx;
 
-   if (idx >= ARRAY_SIZE(hw_addrs))
-   return -1;
if (hw == IVTV_HW_TUNER) {
/* special tuner handling */
sd = v4l2_i2c_new_subdev(&itv->v4l2_dev, adap, type, 0,
-- 
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html