[PATCH 1/3] Initial support for device autodetection.

Giuseppe Bilotta giuseppe.bilotta at gmail.com
Wed Apr 25 00:51:51 PDT 2007


On 4/25/07, Daniel Stone <daniel at fooishbar.org> wrote:
> On Wed, Apr 25, 2007 at 02:44:05AM +0200, Giuseppe Bilotta wrote:
> > +static Bool
> > +fd_query_acecad(int fd) {
> > +     char name[256] = "Unknown";
> > +     char ace_name[7] = "ACECAD\0";
>
> You don't need the trailing \0.

Ah, good to know.

> > +    for (i = 0; ; i++) {
> > +     char fname[64];
> > +     int fd = -1;
> > +     Bool is_acecad;
> > +
> > +     sprintf(fname, "%s/%s%d", DEV_INPUT_EVENT, EVENT_DEV_NAME, i);
>
> You want to use snprintf here.


This is the code that I copied from the synaptics driver. Considering
that the constant string is 16 bytes long, plus the number, I didn't
see how it could overflow (the user would have to have a number of
event devices whose decimal representation is 48 bytes long) so I
didn't worry about altering it. How do you like this patch then?

diff --git a/src/acecad.c b/src/acecad.c
index 84418c2..e121e91 100644
--- a/src/acecad.c
+++ b/src/acecad.c
@@ -198,9 +198,10 @@ fd_query_acecad(int fd) {
 	return FALSE;
 }

-/* Shamelessly copied from synpatics/eventcomm.c */
+/* Shamelessly copied from synpatics/eventcomm.c, with refinements */
 #define DEV_INPUT_EVENT "/dev/input"
 #define EVENT_DEV_NAME "event"
+#define EV_DEV_NAME_MAXLEN 64

 static Bool
 AceCadAutoDevProbe(LocalDevicePtr local)
@@ -212,11 +213,15 @@ AceCadAutoDevProbe(LocalDevicePtr local)
     const int max_skip = 10;

     for (i = 0; ; i++) {
-	char fname[64];
+	char fname[EV_DEV_NAME_MAXLEN];
 	int fd = -1;
 	Bool is_acecad;

-	sprintf(fname, "%s/%s%d", DEV_INPUT_EVENT, EVENT_DEV_NAME, i);
+	int np = snprintf(fname, EV_DEV_NAME_MAXLEN, "%s/%s%d",
DEV_INPUT_EVENT, EVENT_DEV_NAME, i);
+	if (np < 0 || np >= EV_DEV_NAME_MAXLEN) {
+		xf86Msg(X_WARNING, "too many devices, giving up");
+		break;
+	}
 	SYSCALL(fd = open(fname, O_RDONLY));
 	if (fd < 0) {
 	    if (errno == ENOENT) {


> > +    ErrorF("%s no acecad event device found (checked %d nodes)\n",
> > +        local->name, i + 1);
>
> xf86Msg instead?

I was wondering about that myself, but not knowing exactly what the
difference between the various commands were, I left the original code
as is. I'll now checkout the doc module and start reading this stuff
up :)

-- 
Giuseppe "Oblomov" Bilotta



More information about the xorg mailing list