From 9b4e3718ed2ede744fb34b90b3f7b86ff2e01b15 Mon Sep 17 00:00:00 2001 From: Brendan Shanks Date: Tue, 19 Mar 2024 15:15:58 -0700 Subject: [PATCH] server: Replace sprintf with snprintf to avoid deprecation warnings on macOS. --- server/change.c | 2 +- server/directory.c | 2 +- server/fd.c | 12 ++++++------ server/mapping.c | 2 +- server/process.c | 2 +- server/procfs.c | 4 ++-- server/ptrace.c | 4 ++-- server/registry.c | 15 ++++++++------- server/trace.c | 2 +- server/unicode.c | 10 +++++----- 10 files changed, 28 insertions(+), 27 deletions(-) diff --git a/server/change.c b/server/change.c index 843e495411c..f773ccf8831 100644 --- a/server/change.c +++ b/server/change.c @@ -726,7 +726,7 @@ static char *get_path_from_fd( int fd, int sz ) #ifdef linux char *ret = malloc( 32 + sz ); - if (ret) sprintf( ret, "/proc/self/fd/%u", fd ); + if (ret) snprintf( ret, 32 + sz, "/proc/self/fd/%u", fd ); return ret; #elif defined(F_GETPATH) char *ret = malloc( PATH_MAX + sz ); diff --git a/server/directory.c b/server/directory.c index 23d7eb0a2b7..e521a7b38c9 100644 --- a/server/directory.c +++ b/server/directory.c @@ -320,7 +320,7 @@ static void create_session( unsigned int id ) release_object( dir_sessions ); } - sprintf( id_strA, "%u", id ); + snprintf( id_strA, sizeof(id_strA), "%u", id ); id_strW = ascii_to_unicode_str( id_strA, &id_str ); dir_id = create_directory( &dir_sessions->obj, &id_str, 0, HASH_SIZE, NULL ); dir_dosdevices = create_directory( &dir_id->obj, &dir_dosdevices_str, OBJ_PERMANENT, HASH_SIZE, NULL ); diff --git a/server/fd.c b/server/fd.c index 8576882aaa9..fc7f7be92b9 100644 --- a/server/fd.c +++ b/server/fd.c @@ -465,7 +465,7 @@ const char *get_timeout_str( timeout_t timeout ) { secs = -timeout / TICKS_PER_SEC; nsecs = -timeout % TICKS_PER_SEC; - sprintf( buffer, "+%ld.%07ld", secs, nsecs ); + snprintf( buffer, sizeof(buffer), "+%ld.%07ld", secs, nsecs ); } else /* absolute */ { @@ -477,12 +477,12 @@ const char *get_timeout_str( timeout_t timeout ) secs--; } if (secs >= 0) - sprintf( buffer, "%x%08x (+%ld.%07ld)", - (unsigned int)(timeout >> 32), (unsigned int)timeout, secs, nsecs ); + snprintf( buffer, sizeof(buffer), "%x%08x (+%ld.%07ld)", + (unsigned int)(timeout >> 32), (unsigned int)timeout, secs, nsecs ); else - sprintf( buffer, "%x%08x (-%ld.%07ld)", - (unsigned int)(timeout >> 32), (unsigned int)timeout, - -(secs + 1), TICKS_PER_SEC - nsecs ); + snprintf( buffer, sizeof(buffer), "%x%08x (-%ld.%07ld)", + (unsigned int)(timeout >> 32), (unsigned int)timeout, + -(secs + 1), TICKS_PER_SEC - nsecs ); } return buffer; } diff --git a/server/mapping.c b/server/mapping.c index 6b0de1b8b94..ff99b45ce51 100644 --- a/server/mapping.c +++ b/server/mapping.c @@ -292,7 +292,7 @@ static int make_temp_file( char name[16] ) value += (current_time >> 16) + current_time; for (i = 0; i < 0x8000 && fd < 0; i++, value += 7777) { - sprintf( name, "tmpmap-%08x", value ); + snprintf( name, 16, "tmpmap-%08x", value ); fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0600 ); } return fd; diff --git a/server/process.c b/server/process.c index abd6edb58a9..b7c7df38e44 100644 --- a/server/process.c +++ b/server/process.c @@ -1552,7 +1552,7 @@ DECL_HANDLER(get_process_vm_counters) char proc_path[32], line[256]; unsigned long value; - sprintf( proc_path, "/proc/%u/status", process->unix_pid ); + snprintf( proc_path, sizeof(proc_path), "/proc/%u/status", process->unix_pid ); if ((f = fopen( proc_path, "r" ))) { while (fgets( line, sizeof(line), f )) diff --git a/server/procfs.c b/server/procfs.c index 7b61b2abb46..0492a69c94e 100644 --- a/server/procfs.c +++ b/server/procfs.c @@ -55,7 +55,7 @@ static int open_proc_as( struct process *process, int flags ) return -1; } - sprintf( buffer, "/proc/%u/as", process->unix_pid ); + snprintf( buffer, sizeof(buffer), "/proc/%u/as", process->unix_pid ); if ((fd = open( buffer, flags )) == -1) { if (errno == ENOENT) /* probably got killed */ @@ -75,7 +75,7 @@ static int open_proc_lwpctl( struct thread *thread ) if (thread->unix_pid == -1) return -1; - sprintf( buffer, "/proc/%u/lwp/%u/lwpctl", thread->unix_pid, thread->unix_tid ); + snprintf( buffer, sizeof(buffer), "/proc/%u/lwp/%u/lwpctl", thread->unix_pid, thread->unix_tid ); if ((fd = open( buffer, O_WRONLY )) == -1) { if (errno == ENOENT) /* probably got killed */ diff --git a/server/ptrace.c b/server/ptrace.c index 976d3a3778e..bd86f0bf2ec 100644 --- a/server/ptrace.c +++ b/server/ptrace.c @@ -366,7 +366,7 @@ int read_process_memory( struct process *process, client_ptr_t ptr, data_size_t char procmem[24]; int fd; - sprintf( procmem, "/proc/%u/mem", process->unix_pid ); + snprintf( procmem, sizeof(procmem), "/proc/%u/mem", process->unix_pid ); if ((fd = open( procmem, O_RDONLY )) != -1) { ssize_t ret = pread( fd, dest, size, ptr ); @@ -467,7 +467,7 @@ int write_process_memory( struct process *process, client_ptr_t ptr, data_size_t char procmem[24]; int fd; - sprintf( procmem, "/proc/%u/mem", process->unix_pid ); + snprintf( procmem, sizeof(procmem), "/proc/%u/mem", process->unix_pid ); if ((fd = open( procmem, O_WRONLY )) != -1) { ssize_t r = pwrite( fd, src, size, ptr ); diff --git a/server/registry.c b/server/registry.c index e88bed1e72e..5e2c748fa32 100644 --- a/server/registry.c +++ b/server/registry.c @@ -1831,15 +1831,16 @@ static int load_init_registry_from_file( const char *filename, struct key *key ) static WCHAR *format_user_registry_path( const struct sid *sid, struct unicode_str *path ) { - char buffer[7 + 11 + 11 + 11 * ARRAY_SIZE(sid->sub_auth)], *p = buffer; + char buffer[7 + 11 + 11 + 11 * ARRAY_SIZE(sid->sub_auth)]; unsigned int i; + int len; - p += sprintf( p, "User\\S-%u-%u", sid->revision, - ((unsigned int)sid->id_auth[2] << 24) | - ((unsigned int)sid->id_auth[3] << 16) | - ((unsigned int)sid->id_auth[4] << 8) | - ((unsigned int)sid->id_auth[5]) ); - for (i = 0; i < sid->sub_count; i++) p += sprintf( p, "-%u", sid->sub_auth[i] ); + len = snprintf( buffer, sizeof(buffer), "User\\S-%u-%u", sid->revision, + ((unsigned int)sid->id_auth[2] << 24) | + ((unsigned int)sid->id_auth[3] << 16) | + ((unsigned int)sid->id_auth[4] << 8) | + ((unsigned int)sid->id_auth[5]) ); + for (i = 0; i < sid->sub_count; i++) len += snprintf( buffer + len, sizeof(buffer) - len, "-%u", sid->sub_auth[i] ); return ascii_to_unicode_str( buffer, path ); } diff --git a/server/trace.c b/server/trace.c index a94b535bd5d..efb4bb03c4a 100644 --- a/server/trace.c +++ b/server/trace.c @@ -5645,7 +5645,7 @@ static const char *get_status_name( unsigned int status ) for (i = 0; status_names[i].name; i++) if (status_names[i].value == status) return status_names[i].name; } - sprintf( buffer, "%x", status ); + snprintf( buffer, sizeof(buffer), "%x", status ); return buffer; } diff --git a/server/unicode.c b/server/unicode.c index abc3367be75..cae9df7da65 100644 --- a/server/unicode.c +++ b/server/unicode.c @@ -212,19 +212,19 @@ int dump_strW( const WCHAR *str, data_size_t len, FILE *f, const char escape[2] if (*str > 127) /* hex escape */ { if (len > 1 && str[1] < 128 && isxdigit((char)str[1])) - pos += sprintf( pos, "\\x%04x", *str ); + pos += snprintf( pos, sizeof(buffer) - (pos - buffer), "\\x%04x", *str ); else - pos += sprintf( pos, "\\x%x", *str ); + pos += snprintf( pos, sizeof(buffer) - (pos - buffer), "\\x%x", *str ); continue; } if (*str < 32) /* octal or C escape */ { if (escapes[*str] != '.') - pos += sprintf( pos, "\\%c", escapes[*str] ); + pos += snprintf( pos, sizeof(buffer) - (pos - buffer), "\\%c", escapes[*str] ); else if (len > 1 && str[1] >= '0' && str[1] <= '7') - pos += sprintf( pos, "\\%03o", *str ); + pos += snprintf( pos, sizeof(buffer) - (pos - buffer), "\\%03o", *str ); else - pos += sprintf( pos, "\\%o", *str ); + pos += snprintf( pos, sizeof(buffer) - (pos - buffer), "\\%o", *str ); continue; } if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\';