On Tue, Aug 27, 2002 at 12:32:27PM -0500, Rick Matthews wrote:
> > -----Original Message-----
> > From: Gilles CHAUVIN
> > Sent: Tuesday, August 27, 2002 10:36 AM
> >
> > Maybe you've already read my two previous messages about that very
> > annoying bug in squidGuard:
> > http://marc.theaimsgroup.com/?l=squidguard&m=102372008217944&w=2
> > http://marc.theaimsgroup.com/?l=squidguard&m=103011521422745&w=2
> 
> Yes, I've read your posts about it, and even ran a few tests on ideas
> that I had. Ultimately though, I didn't have any answers or
> suggestions for you.
> 
> > I know other people do have the same problem with squidGuard when
> > they're trying to update their databases.
> 
> I've never had the problem, and I'd never read about it until I
> read your post. That's one reason why I find it so curious. Why
> are so few people having the problem? What is it that those systems
> have in common, that triggers the problem?
> 
Looking at the ML archives gives at least two other posts that talk
about this problem (that's the two posts I can remember talking about
this, maybe there are others):
http://marc.theaimsgroup.com/?l=squidguard&m=102008640828326&w=2
http://marc.theaimsgroup.com/?l=squidguard&m=102636248825888&w=2

> > With the help of a colleague, we think we've isolated this bug.
> 
> Cool!
> 
> > To solve that, edit the file ... Go to line 44 and replace ...
> > Next, go to line 146 and replace ... 
> > Now recompile squidGuard and try.... 
> > It should work without any problems
> > (that's the case for me ;).
> 
> Is that's all the information that you are going to give us? How about
> a comment or two about what you found to be the cause? Or how you
> tested to make sure that was, in fact, the cause? And why so few people
> are experiencing the problem?
> 
> I'm looking forward to reading more about it!
> 
Ok, so... I'll try to explain this (with my wonderful english ;).

Doing a "strace" when you launch squidGuard for a database update gives
something like:

# strace squidGuard -u -d

[...]

open("/var/squidGuard/hacking/domains.diffq`", O_RDONLY) = -1 ENOENT (No
such file or directory)
time(NULL)                              = 1030520704
write(2, "2002-08-28 09:45:04 [4176] /var/"..., 932002-08-28 09:45:04
[4176] /var/squidGuard/hacking/domains.diff`c: Noh file or directory
) = 93


So I decided to grep the source code and search for the ".diff" string
(thinking the bug would be around the word ".diff" somewhere in the
source code). It leads me to the file "sgDb.c".

Now, assuming I don't know anything about C programming (as I said in my
previous message ;) but have very basic skills about programming, I
tried to look at the code and see this:

        update = (char *) sgMalloc(strlen(file) + 5);
        strcpy(update,file);
        strcat(update,".diff");

As I don't know C, I tried to understand, step by step, what those 3
lines could mean. Start from now, I'll probably say stupid things
(that's how I understand things, not how they really works ;) don't
hesitate to tell me if I'm wrong.

"update = (char *) sgMalloc(strlen(file) + 5);" reserve memory to store
the file name (eg. "domains", "urls") + the file extension which is
".diff". I've read the man pages for "strcpy" and "strcat" to figure out
what these functions are made for.

If you read the man page for "strcpy" you could read this:
SYNOPSIS
       #include <string.h>
       char *strcpy(char *dest, const char *src);
       char *strncpy(char *dest, const char *src, size_t n);

DESCRIPTION
       The strcpy() function copies the string pointed to by src (including the 
terminating `\0' character)
       to the array pointed to by dest. [...]

It says "including the '\0' character". So, in the above line you have
to change "5" by "6" to let room for the ending '\0 (NUL)' character.


When trying to understand where the bug was, I went to this part of the
code (which is very similar to the 3 lines above):
    dbfile = (char *) sgMalloc(strlen(file) + 5);
    strcpy(dbfile,file);
    strcat(dbfile,".db");

And I thought, if I'm right, we have to change "5" by "4" since ".db\0"
is only 4 bytes long.

I've done those mods in the code:
-----------------------------------------------------------------------
--- sgDb.c.orig Wed Aug 28 09:29:35 2002
+++ sgDb.c      Wed Aug 28 09:29:51 2002
@@ -41,7 +41,7 @@
     if(globalCreateDb != NULL && (!strcmp(globalCreateDb,"all") || 
        !sgStrRncmp(file,globalCreateDb,strlen(globalCreateDb))))
       createdb = 1;
-    dbfile = (char *) sgMalloc(strlen(file) + 5);
+    dbfile = (char *) sgMalloc(strlen(file) + 4);
     strcpy(dbfile,file);
     strcat(dbfile,".db");
     if(stat(dbfile,&st) == 0){
@@ -143,7 +143,7 @@
       if(dbfile == NULL){
         sgLogError("error update dbfile %s.db. file does not exists,
use -C to create",file);
       } else {
-        update = (char *) sgMalloc(strlen(file) + 5);
+        update = (char *) sgMalloc(strlen(file) + 6);
         strcpy(update,file);
         strcat(update,".diff");
         if(stat(update,&st) == 0){
-----------------------------------------------------------------------

Now, if I recompile and execute squidGuard, all is working fine, without
any more problems.

Here is my explanation.... Hope you understand now how I went to modify
these parts of the code.

If I done something stupid, don't hesitate to tell me (even if it's
stupid, it works... ;)

> Rick

Regards,
Gilles.
-- 
/ Gilles CHAUVIN / CRDP de Haute-Normandie /
/ E-mail: [EMAIL PROTECTED] / GnuPG: 0x2E89DBB7 /

Attachment: msg01183/pgp00000.pgp
Description: PGP signature

Reply via email to