Author: Steffen Moeller <moeller@debian.org>
LastChanged: Sat, 12 Jun 2010 16:03:22 +0200
Description: strcpy -> strncpy fixes

--- a/ncoils.c
+++ b/ncoils.c
@@ -35,7 +35,8 @@ main(int argc, char *argv[]) {
 	int mode;
 	int min_seg;
 
-	char heptfile[1000];
+#define HEPTFILELENGTH 1000
+	char heptfile[HEPTFILELENGTH];
 	char *buff;
 	static char *env;
 	char *seq,*title,*ident;
@@ -56,10 +57,12 @@ main(int argc, char *argv[]) {
 
 	if((env=getenv("COILSDIR"))==NULL) {
 		fprintf(stderr,"error: environment variable COILSDIR must be set\n");
+		fprintf(stderr,"       Assuming /usr/share/ncoils\n");
+		env=strdup("/usr/share/ncoils"); // a little leak, tolerated as singleton
 		exit(-1);
 	}
 
-	strcpy(&heptfile[0],env);
+	strncpy(&heptfile[0],env,HEPTFILELENGTH-1-8); // -1 for terminal 0, 8 for "/new.mat"
 	strcpy(&heptfile[strlen(heptfile)],"/new.mat");
 
 
@@ -67,7 +70,7 @@ main(int argc, char *argv[]) {
            if(argv[i][0]!='-') exit_error();
 	   if(strcmp(&argv[i][1],"m")==0) {
              if((i+1)>=argc) exit_error();
-             strcpy(&heptfile[0],argv[i+1]);
+             strncpy(&heptfile[0],argv[i+1],HEPTFILELENGTH-1);
              i++;
 	   } else if(strcmp(&argv[i][1],"win")==0) {
              if((i+1)>=argc) exit_error();
@@ -159,6 +162,10 @@ main(int argc, char *argv[]) {
 		} else {
 /*			printf("Adding |%s| to |%s| = \n",buff,seq);  */
 			seq=(char*)realloc(seq,(seqlen+strlen(buff)+1)*sizeof(char));
+			if (NULL == seq) {
+				fprintf(stderr,"Could not allocate memory.\n");
+				exit(-1);
+			}
 		        strcpy(&seq[seqlen],buff); 
                         seqlen=strlen(seq);
 /*			printf("       |%s|\n",seq);  */
--- a/ncoils.h
+++ b/ncoils.h
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
+#include <string.h>
 
 #define AAs "A_CDEFGHI_KLMN_PQRST_VW_Y_"
 #define PI  3.1415
