uss: more gcc9 truncation warning appeasement
[openafs.git] / src / uss / uss_procs.c
index a5be915..ff5619f 100644 (file)
@@ -144,12 +144,10 @@ uss_procs_BuildDir(char *a_path, char *a_mode, char *a_owner, char *a_access)
      * Use our linked list to remember this directory's true ACL setting so
      * we may set it correctly at the tail end of the account creation.
      */
-    new_dir = (struct uss_subdir *)malloc(sizeof(struct uss_subdir));
+    new_dir = malloc(sizeof(struct uss_subdir));
     new_dir->previous = uss_currentDir;
-    new_dir->path = (char *)malloc(strlen(a_path) + 1);
-    strcpy(new_dir->path, a_path);
-    new_dir->finalACL = (char *)malloc(strlen(a_access) + 1);
-    strcpy(new_dir->finalACL, a_access);
+    new_dir->path = strdup(a_path);
+    new_dir->finalACL = strdup(a_access);
     uss_currentDir = new_dir;
 
     /*
@@ -508,7 +506,7 @@ Copy(char *a_from, char *a_to, int a_mode)
 
     int fd1, fd2;
     char buf[BUFSIZ];
-    int cnt, rc;
+    int rcnt, wcnt = -1, rc;
 
     umask(0);
     fd1 = open(a_to, O_EXCL | O_CREAT | O_WRONLY, a_mode);
@@ -539,10 +537,18 @@ Copy(char *a_from, char *a_to, int a_mode)
        close(fd1);
        return (1);
     }
-    while ((cnt = read(fd2, buf, BUFSIZ)) == BUFSIZ)
-       write(fd1, buf, cnt);
-
-    write(fd1, buf, cnt);
+    do {
+       rcnt = read(fd2, buf, BUFSIZ);
+       if (rcnt == -1)
+           break;
+       wcnt = write(fd1, buf, rcnt);
+    } while (rcnt == BUFSIZ && rcnt == wcnt);
+    if (rcnt == -1 || wcnt != rcnt) {
+       uss_procs_PrintErr(line, "read/write error to %s\n", a_to);
+       close(fd1);
+       close(fd2);
+       return (1);
+    }
     rc = close(fd1);
     if (rc) {
        uss_procs_PrintErr(line, "Failed to close '%s' %s\n", a_to,
@@ -610,8 +616,12 @@ Echo(char *a_s, char *a_f, int a_mode)
            return (1);
        }
     }
-    write(fd, a_s, strlen(a_s));
-    write(fd, "\n", 1);
+    if (write(fd, a_s, strlen(a_s)) != strlen(a_s) ||
+       write(fd, "\n", 1) != 1) {
+       uss_procs_PrintErr(line, "Short write to '%s'\n", a_f);
+       close(fd);
+       return (1);
+    }
     if (close(fd)) {
        uss_procs_PrintErr(line, "Failed to close '%s': %s\n", a_f,
                           strerror(errno));
@@ -649,12 +659,12 @@ afs_int32
 uss_procs_PickADir(char *path, char *cp)
 {                              /*uss_procs_PickADir */
 
-    char cd[300];              /*Current  directory for search */
+    char cd[uss_DirPoolLen];           /*Current  directory for search */
 
     int i, count, MinIndex = 0, mina = 10000;
     struct dirent *dp;
     DIR *dirp;
-    char dirname[300];
+    char dirname[uss_DirPoolLen*2];
 
     if (uss_NumGroups == 0) {
        fprintf(stderr, "%s: No choice yet given to replace $AUTO\n",
@@ -688,7 +698,7 @@ uss_procs_PickADir(char *path, char *cp)
      * each and pick the minimum.
      */
     for (i = 0; i < uss_NumGroups; i++) {
-       sprintf(dirname, "%s/%s", cd, uss_DirPool[i]);
+       snprintf(dirname, sizeof(dirname), "%s/%s", cd, uss_DirPool[i]);
        if ((dirp = opendir(dirname)) == NULL) {
            if (errno != ENOTDIR)
                fprintf(stderr,
@@ -761,7 +771,9 @@ uss_procs_FindAndOpen(char *a_fileToOpen)
 
     FILE *rv;                  /*Template file descriptor */
     int i;                     /*Loop counter */
-    char tmp_str[uss_MAX_SIZE];        /*Tmp string */
+    char *tmp_str;             /*Points to the name of the file */
+                               /* -> a_fileToOpen or -> buffer @tmp_str_free */
+    char *tmp_str_free = NULL;  /*Dynamically built filename */
     static char
       TemplatePath[NUM_TPL_PATHS][1024];       /*Template directories */
     int cant_read;             /*Can't read the file? */
@@ -770,13 +782,14 @@ uss_procs_FindAndOpen(char *a_fileToOpen)
      * If a full pathname was given, just take it as is.
      */
     if (strchr(a_fileToOpen, '/')) {
-       strcpy(tmp_str, a_fileToOpen);
+       tmp_str = a_fileToOpen;
        rv = fopen(a_fileToOpen, "r");
     } else {
        /*
         * A relative pathname was given.  Try to find the file in each of
         * the default template directories.
         */
+       int mem_error = 0;
        cant_read = 0;
 
        sprintf(TemplatePath[0], "%s", ".");
@@ -784,7 +797,19 @@ uss_procs_FindAndOpen(char *a_fileToOpen)
        sprintf(TemplatePath[2], "%s", "/etc");
 
        for (i = 0; i < NUM_TPL_PATHS; i++) {
-           sprintf(tmp_str, "%s/%s", TemplatePath[i], a_fileToOpen);
+           int code;
+           free(tmp_str_free);
+           tmp_str_free = NULL;
+           code = asprintf(&tmp_str_free, "%s/%s",
+                           TemplatePath[i], a_fileToOpen);
+           if (code == -1) {
+               tmp_str_free = NULL;
+               mem_error = 1;
+               rv = NULL;
+               break;
+           }
+           tmp_str = tmp_str_free;
+
            if ((rv = fopen(tmp_str, "r")) != NULL)
                break;
 
@@ -811,7 +836,11 @@ uss_procs_FindAndOpen(char *a_fileToOpen)
             * Check to see if we specifically found the file but
             * couldn't read it.
             */
-           if (cant_read)
+           if (mem_error) {
+               fprintf(stderr, "%s: Error allocating memory\n",
+                       uss_whoami);
+           }
+           else if (cant_read)
                fprintf(stderr, "%s: Can't open template '%s': %s\n",
                        uss_whoami, tmp_str, strerror(errno));
            else {
@@ -827,6 +856,7 @@ uss_procs_FindAndOpen(char *a_fileToOpen)
     /*
      * Whatever happened, return what we got.
      */
+    free(tmp_str_free);
     return (rv);
 
 }                              /*uss_procs_FindAndOpen */