@@ -422,10 +422,7 @@ struct rank_info {
422422
423423static void get_host_name (char * hostname, int maxlen, const char delim)
424424{
425- if (gethostname (hostname, maxlen) != 0 ) {
426- strncpy (hostname, " unknown" , maxlen);
427- WHOLEMEMORY_FATAL (" gethostname failed." );
428- }
425+ if (gethostname (hostname, maxlen) != 0 ) { WHOLEMEMORY_FATAL (" gethostname failed." ); }
429426 int i = 0 ;
430427 while ((hostname[i] != delim) && (hostname[i] != ' \0 ' ) && (i < maxlen - 1 ))
431428 i++;
@@ -448,22 +445,25 @@ void get_boot_id(char* host_id, size_t len)
448445
449446 if ((env_host_id = getenv (" WHOLEMEMORY_BOOTID" )) != nullptr ) {
450447 WHOLEMEMORY_LOG (LEVEL_INFO, " WHOLEMEMORY_BOOTID set by environment to %s" , env_host_id);
451- strncpy (host_id, env_host_id, len - 1 );
452- offset = strlen (env_host_id);
448+ size_t copy_len = std::min (strlen (env_host_id), len - 1 );
449+ memcpy (host_id, env_host_id, copy_len);
450+ offset = copy_len;
453451 } else {
454452 FILE* file = fopen (BOOTID_FILE, " r" );
455453 if (file != nullptr ) {
456454 char * p;
457455 if (fscanf (file, " %ms" , &p) == 1 ) {
458- strncpy (host_id + offset, p, len - offset - 1 );
459- offset += strlen (p);
456+ size_t remaining = len - offset - 1 ;
457+ size_t copy_len = std::min (strlen (p), remaining);
458+ memcpy (host_id + offset, p, copy_len);
459+ offset += copy_len;
460460 free (p);
461461 }
462+ fclose (file);
462463 }
463- fclose (file);
464464 }
465465
466- #undef HOSTID_FILE
466+ #undef BOOTID_FILE
467467
468468 host_id[offset] = ' \0 ' ;
469469}
0 commit comments