Skip to contentSkip to conversation

Properly replace an existing key value pair in a text file

PATCH In review Ville Nuorvala 2 participants 4 messages Sep 7, 2005 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Properly replace an existing key value pair in a text file

Hi,

textfile_put() was unable to replace a key value pair if it happened to be the last entry in a file. I noticed this problem in bluez-utils-2.19, but it still seems to exist in the latest CVS code. The patch below should fix it...

Index: common/textfile.c
===================================================================
RCS file: /cvsroot/bluez/utils/common/textfile.c,v retrieving revision 1.6

textfile.c 7 Sep 2005 11:06:43 -0000 renamed from textfile.c 30 Aug 2005 00:35:39 -0000 1.6 +8 -2
diff -u -r1.6 textfile.c
@@ -180,8 +180,14 @@
        end += len;
 
        len = size - (end - map);
-
-       if (len <= 0 || len > size) {
+       if (!len) {
+               munmap(map, size);
+               ftruncate(fd, base);
+               pos = lseek(fd, base, SEEK_SET);
+               err = write_key_value(fd, key, value);
+               goto unlock;
+       }
+       if (len < 0 || len > size) {
                err = EILSEQ;
                goto unmap;
        }

Regards,
Ville

Signature · 5 lines
Ville Nuorvala
Research Assistant,
Laboratory for Theoretical Computer Science,
Helsinki University of Technology
email: [email protected], phone: +358 (0)9 451 5257

begin:vcard
fn:Ville Nuorvala
n:Nuorvala;Ville
adr:;;Helsinginkatu 17 A 18;Helsinki;;FIN-00500;Finland email;internet:[email protected] tel;work:+35894515257
tel;fax:+35894513369
tel;cell:+358405835323
x-mozilla-html:FALSE
version:2.1
end:vcard

maintainer
#1 lore raw

Hi Ville,

Quoted 4 lines

textfile_put() was unable to replace a key value pair if it happened to be the last entry in a file. I noticed this problem in bluez-utils-2.19, but it still seems to exist in the latest CVS code. The patch below should fix it...

I though that I fixed this. Are you sure that problem is still present?

Regards

Marcel

-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Bluez-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel

#2 lore raw
Quoted 7 lines · Marcel Holtmann

Hi Ville,

Quoted 1 line

textfile_put() was unable to replace a key value pair if it happened to be the last entry in a file. I noticed this problem in bluez-utils-2.19, but it still seems to exist in the latest CVS code. The patch below should fix it...

I though that I fixed this. Are you sure that problem is still present?

Yes. I located the problem in Gentoo's bluez-utils-2.19 and since it hasn't yet been fixed in the CVS repository, I'm quite certain it still is present ;-)

I noticed the problem when hcid just wouldn't update the link key in var/lib/bluetooth/<hwaddr>/linkkeys, even though the debug statements claimed otherwise. The textfile_put() function clearly works incorrectly since zero is a valid value for the len variable, but the if (len <= 0 || ..) check causes the the function to exit before anything is written.

Btw, I resend the patch as a separate attachement since the tabs got screwed up last time.

Regards,
Ville

Signature · 5 lines
Ville Nuorvala
Research Assistant,
Laboratory for Theoretical Computer Science,
Helsinki University of Technology
email: [email protected], phone: +358 (0)9 451 5257

Index: common/textfile.c
===================================================================
RCS file: /cvsroot/bluez/utils/common/textfile.c,v retrieving revision 1.6

textfile.c +8 -2
diff -u -r1.6 textfile.c
@@ -180,8 +180,14 @@
 	end += len;
 
 	len = size - (end - map);
-
-	if (len <= 0 || len > size) {
+	if (!len) {
+		munmap(map, size);
+		ftruncate(fd, base);
+		pos = lseek(fd, base, SEEK_SET);
+		err = write_key_value(fd, key, value);
+		goto unlock;
+	}
+	if (len < 0 || len > size) {
 		err = EILSEQ;
 		goto unmap;
 	}
maintainer
#3 lore raw

Hi Ville,

Quoted 9 lines

Yes. I located the problem in Gentoo's bluez-utils-2.19 and since it hasn't yet been fixed in the CVS repository, I'm quite certain it still is present ;-)

I noticed the problem when hcid just wouldn't update the link key in var/lib/bluetooth/<hwaddr>/linkkeys, even though the debug statements claimed otherwise. The textfile_put() function clearly works incorrectly since zero is a valid value for the len variable, but the if (len <= 0 || ..) check causes the the function to exit before anything is written.

this sounds plausible. Your patch is applied to the CVS. Thanks.

Regards

Marcel

-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Bluez-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel