gnunet-developers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [GNUnet-developers] Patch: use strncpy and strncat rather than strc


From: Christian Grothoff
Subject: Re: [GNUnet-developers] Patch: use strncpy and strncat rather than strcpy and strcat (pedantic defensive programming)
Date: Thu, 27 Feb 2003 15:47:28 -0500
User-agent: KMail/1.4.3

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I think string operations in C are always problematic, and a generic rule like 
"replace strcat with strncat" does not really help. In either case, we must 
pay attention to do it right, and personally I fail to see that one way has 
an advantage over the other. Only in this case, your patch adds some 
additional arguments, locals and overall just makes the code a bit longer. 

n = strlen(y);
x = malloc(n);
strncpy(x, y, n); 

or

x = malloc(strlen(y));
strcpy(x, y);

does not make any difference, only the first is longer. Note that the code 
above is intentionally broken in both cases just to make my point. :-)

Christian

On Tuesday 18 February 2003 12:10 pm, address@hidden wrote:
> ? diff.txt
> Index: src/applications/afs/database/contentdatabase_directory.c
> ===================================================================
> RCS file:
> /var/cvs/GNUnet/GNUnet/src/applications/afs/database/contentdatabase_direct
>ory.c,v retrieving revision 1.14
> diff -r1.14 contentdatabase_directory.c
> 163a164
>
> >   int filSize;
>
> 168,170c169,172
> <   fil = xmalloc(strlen(dbh) + strlen((char*)&fn) + 1,
> <             "readContent: filename");
> <   buildFileName(dbh, &fn, fil);
> ---
>
> >   filSize = strlen(dbh) + strlen((char*)&fn) + 1;
> >   fil = xmalloc(filSize, "readContent: filename");
> >   buildFileName(dbh, &fn, fil, filSize);
>
> 203a206
>
> >   int filSize = 0;
>
> 206,208c209,211
> <   fil = xmalloc(strlen(dbh) + strlen((char*)&fn) + 1,
> <             "writeContent: filename");
> <   buildFileName(dbh, &fn, fil);
> ---
>
> >   filSize = strlen(dbh) + strlen((char*)&fn) + 1;
> >   fil = xmalloc(filSize, "writeContent: filename");
> >   buildFileName(dbh, &fn, fil, filSize);
>
> 231a235
>
> >   int filSize;
>
> 236,238c240,242
> <   fil = xmalloc(strlen(dbh) + strlen((char*)&fn) + 1,
> <             "unlinkFromDB: filename");
> <   buildFileName(dbh, &fn, fil);
> ---
>
> >   filSize = strlen(dbh) + strlen((char*)&fn) + 1;
> >   fil = xmalloc(filSize, "unlinkFromDB: filename");
> >   buildFileName(dbh, &fn, fil, filSize);
>
> Index: src/applications/afs/database/contentdatabase_gdbm.c
> ===================================================================
> RCS file:
> /var/cvs/GNUnet/GNUnet/src/applications/afs/database/contentdatabase_gdbm.c
>,v retrieving revision 1.19
> diff -r1.19 contentdatabase_gdbm.c
> 75a76
>
> >   int ffSize;
>
> 84,86c85,87
> <   ff = xmalloc(strlen(dir)+strlen(GDB_EXT)+1,
> <            "getDatabase: filename");
> <   strcpy(ff, dir);
> ---
>
> >   ffSize = strlen(dir)+strlen(GDB_EXT)+1;
> >   ff = xmalloc(ffSize, "getDatabase: filename");
> >   strncpy(ff, dir, ffSize - 1);
>
> Index: src/applications/afs/database/contentdatabase_tdb.c
> ===================================================================
> RCS file:
> /var/cvs/GNUnet/GNUnet/src/applications/afs/database/contentdatabase_tdb.c,
>v retrieving revision 1.13
> diff -r1.13 contentdatabase_tdb.c
> 86c86
> <   strcpy(ff, dir);
> ---
>
> >   strncpy(ff, dir, fnSize - 1);
>
> Index: src/applications/afs/database/lookup.c
> ===================================================================
> RCS file: /var/cvs/GNUnet/GNUnet/src/applications/afs/database/lookup.c,v
> retrieving revision 1.42
> diff -r1.42 lookup.c
> 80a81
>
> >   int fileNameDottedSize;
>
> 86c87,88
> <   fileNameDotted = xmalloc(strlen(fileName)+3,
> ---
>
> >   fileNameDottedSize = strlen(fileName)+3;
> >   fileNameDotted = xmalloc(fileNameDottedSize,
>
> 97,98c99,101
> <     strcpy(fileNameDotted,
> <        fileName);
> ---
>
> >     strncpy(fileNameDotted,
> >        fileName,
> >        fileNameDottedSize - 1);
>
> Index: src/applications/afs/encoding/block.c
> ===================================================================
> RCS file: /var/cvs/GNUnet/GNUnet/src/applications/afs/encoding/block.c,v
> retrieving revision 1.38
> diff -r1.38 block.c
> 74a75
>
> >   int fnSize;
>
> 87c88,89
> <     fn = xmalloc(strlen(filename) + 3,
> ---
>
> >     fnSize = strlen(filename) + 3;
> >     fn = xmalloc(fnSize,
>
> 89c91
> <     strcpy(fn, filename);
> ---
>
> >     strncpy(fn, filename, fnSize - 1);
>
> 193a196
>
> >   int fnSize;
>
> 201c204,205
> <       fn = xmalloc(strlen(this->filename) + 3,
> ---
>
> >       fnSize = strlen(this->filename) + 3;
> >       fn = xmalloc(fnSize,
>
> 203c207
> <       strcpy(fn, this->filename);
> ---
>
> >       strncpy(fn, this->filename, fnSize - 1);
>
> Index: src/applications/afs/encoding/searchutil.c
> ===================================================================
> RCS file:
> /var/cvs/GNUnet/GNUnet/src/applications/afs/encoding/searchutil.c,v
> retrieving revision 1.25
> diff -r1.25 searchutil.c
> 456a457
>
> >   int tmpSize;
>
> 470,471c471,472
> <       tmp = xmalloc(strlen(keywords[i])+
> <                 strlen(keywords[i+1])+2,
> ---
>
> >       tmpSize =  strlen(keywords[i])+ strlen(keywords[i+1])+2;
> >       tmp = xmalloc(tmpSize,
>
> 473,474c474
> <       tmp[0] = '\0';
> <       strcat(tmp, keywords[i]);
> ---
>
> >       strncpy(tmp, keywords[i], tmpSize - 2);
>
> 476c476
> <       strcat(tmp, keywords[i+1]);
> ---
>
> >       strncat(tmp, keywords[i+1], tmpSize - strlen(tmp) - 1);
>
> Index: src/include/util/storage.h
> ===================================================================
> RCS file: /var/cvs/GNUnet/GNUnet/src/include/util/storage.h,v
> retrieving revision 1.42
> diff -r1.42 storage.h
> 98a99
>
> >  * @param resultBufferSize size of the result buffer
>
> 102c103,104
> <                char * result);
> ---
>
> >                char * result,
> >                int resultBufferSize);
>
> Index: src/server/connection.c
> ===================================================================
> RCS file: /var/cvs/GNUnet/GNUnet/src/server/connection.c,v
> retrieving revision 1.252
> diff -r1.252 connection.c
> 294a295
>
> >   int fnSize;
>
> 301,302c302,303
> <   fn = xmalloc(strlen((char*)trustDirectory)+sizeof(HexName)+1,
> <            "writeHost: filename");
> ---
>
> >   fnSize = strlen((char*)trustDirectory)+sizeof(HexName)+1;
> >   fn = xmalloc(fnSize, "writeHost: filename");
>
> 305c306,307
> <             fn);
> ---
>
> >             fn,
> >             fnSize);
>
> 552a555
>
> >   int len = 0;
>
> 556,558c559,561
> <   fn = xmalloc(strlen((char*)trustDirectory)+sizeof(HexName)+1,
> <            "readHost: filename");
> <   buildFileName(trustDirectory, &fil, fn);
> ---
>
> >   len = strlen((char*)trustDirectory)+sizeof(HexName)+1;
> >   fn = xmalloc(len, "readHost: filename");
> >   buildFileName(trustDirectory, &fil, fn, len);
>
> Index: src/server/knownhosts.c
> ===================================================================
> RCS file: /var/cvs/GNUnet/GNUnet/src/server/knownhosts.c,v
> retrieving revision 1.57
> diff -r1.57 knownhosts.c
> 589a590
>
> >   int fullnameSize;
>
> 608c609,610
> <     fullname = xmalloc(strlen(filename) + strlen(networkIdDirectory) + 1,
> ---
>
> >     fullnameSize = strlen(filename) + strlen(networkIdDirectory) + 1;
> >     fullname = xmalloc(fullnameSize,
>
> 610,612c612,613
> <     fullname[0] = '\0';
> <     strcat(fullname, networkIdDirectory);
> <     strcat(fullname, filename);
> ---
>
> >     strncpy(fullname, networkIdDirectory, fullnameSize - 1);
> >     strncat(fullname, filename, fullnameSize - strlen(fullname) - 1);
>
> Index: src/transports/smtp.c
> ===================================================================
> RCS file: /var/cvs/GNUnet/GNUnet/src/transports/smtp.c,v
> retrieving revision 1.33
> diff -r1.33 smtp.c
> 677,678c677,679
> <   strcpy(&haddr->filter[0],
> <      filter);
> ---
>
> >   strncpy(&haddr->filter[0],
> >      filter,
> >      FILTER_STRING_SIZE - 1);
>
> Index: src/util/configuration.c
> ===================================================================
> RCS file: /var/cvs/GNUnet/GNUnet/src/util/configuration.c,v
> retrieving revision 1.22
> diff -r1.22 configuration.c
> 100a101
>
> >   int resultSize;
>
> 117,118c118,119
> <   result = xmalloc(strlen(prefix) +
> <                strlen(&orig[i+1]) + 2,
> ---
>
> >   resultSize = strlen(prefix) + strlen(&orig[i+1]) + 2;
> >   result = xmalloc(resultSize,
>
> 120c121
> <   strcpy(result, prefix);
> ---
>
> >   strncpy(result, prefix, resultSize - 2);
>
> 122c123
> <   strcat(result, &orig[i+1]);
> ---
>
> >   strncat(result, &orig[i+1], resultSize - strlen(result) - 1);
>
> Index: src/util/dso.c
> ===================================================================
> RCS file: /var/cvs/GNUnet/GNUnet/src/util/dso.c,v
> retrieving revision 1.3
> diff -r1.3 dso.c
> 34a35
>
> >   int libnameSize;
>
> 44c45
> <   libname = xmalloc(strlen(dso) +
> ---
>
> >   libnameSize = strlen(dso) +
>
> 46c47,49
> <                 strlen(prefix) + 1,
> ---
>
> >                 strlen(prefix) + 1;
> >
> >   libname = xmalloc(libnameSize,
>
> 48,51c51,53
> <   libname[0] = '\0';
> <   strcat(libname, prefix);
> <   strcat(libname, dso);
> <   strcat(libname, dsoext);
> ---
>
> >   strncpy(libname, prefix, libnameSize - 1);
> >   strncat(libname, dso, libnameSize - strlen(libname) - 1);
> >   strncat(libname, dsoext, libnameSize - strlen(libname) - 1);
>
> 82a85,88
>
> >   int initNameSize;
> >
> >   initNameSize = strlen(dsoname) +
> >                  strlen(methodprefix) + 1;
>
> 84,85c90
> <   initName = xmalloc(strlen(dsoname) +
> <                  strlen(methodprefix) + 1,
> ---
>
> >   initName = xmalloc(initNameSize,
>
> 87,89c92,93
> <   initName[0] = '\0';
> <   strcat(initName, methodprefix);
> <   strcat(initName, dsoname);
> ---
>
> >   strncpy(initName, methodprefix, initNameSize - 1);
> >   strncat(initName, dsoname, initNameSize - strlen(initName) - 1);
>
> Index: src/util/statistics.c
> ===================================================================
> RCS file: /var/cvs/GNUnet/GNUnet/src/util/statistics.c,v
> retrieving revision 1.26
> diff -r1.26 statistics.c
> 235,236c235,237
> <       strcpy(&statMsg->descriptions[mpos],
> <          descriptions[pos]);
> ---
>
> >       strncpy(&statMsg->descriptions[mpos],
> >          descriptions[pos],
> >          MAX_BUFFER_SIZE - sizeof(STATS_CS_MESSAGE) - mpos);
>
> Index: src/util/storage.c
> ===================================================================
> RCS file: /var/cvs/GNUnet/GNUnet/src/util/storage.c,v
> retrieving revision 1.106
> diff -r1.106 storage.c
> 49a50
>
> >   int fnSize;
>
> 54c55,56
> <     fn = xmalloc(strlen(filename) + strlen(dirname) + 2,
> ---
>
> >     fnSize = strlen(filename) + strlen(dirname) + 2;
> >     fn = xmalloc(fnSize,
>
> 58c60
> <       strcat(fn, dirname);
> ---
>
> >       strncpy(fn, dirname, fnSize - 1);
>
> 64c66
> <       strcat(fn, &filename[1]);
> ---
>
> >       strncat(fn, &filename[1], fnSize - strlen(filename) - 2);
>
> 66c68
> <       strcat(fn, filename);
> ---
>
> >       strncat(fn, filename, fnSize - strlen(filename) - 1);
>
> 281a284
>
> >  * @param resultBufferSize size of the result buffer
>
> 285c288,289
> <                char * result) {
> ---
>
> >                char * result,
> >                int resultBufferSize) {
>
> 288,289c292,294
> <   strcpy(result, dir);
> <   strcat(result, (char*)fil);
> ---
>
> >   memset(result, 0, resultBufferSize);
> >   strncpy(result, dir, resultBufferSize - 1);
> >   strncat(result, (char*)fil, (resultBufferSize - strlen(result) -1));
>
> Concerned about your privacy? Follow this link to get
> FREE encrypted email: https://www.hushmail.com/?l=2
>
> Big $$$ to be made with the HushMail Affiliate Program:
> https://www.hushmail.com/about.php?subloc=affiliate&l=427
>
>
> _______________________________________________
> GNUnet-developers mailing list
> address@hidden
> http://mail.gnu.org/mailman/listinfo/gnunet-developers
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE+Xnlg9tNtMeXQLkIRAgQIAKCDlsJKQZVKKxKImLehofABhx+puwCfU5A7
Yx4kslHGmJtjCuL4zn5NQ5c=
=hkkV
-----END PGP SIGNATURE-----





reply via email to

[Prev in Thread] Current Thread [Next in Thread]