This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFA] win32-nat printf and sprintf removal


>>@@ -1763,9 +1763,9 @@ cygwin_pid_to_str (ptid_t ptid)
> 
>> > int pid = PIDGET (ptid);
>> > > > if ((DWORD) pid == current_event.dwProcessId)
>> >-    sprintf (buf, "process %d", pid);
>> >+    xaprintf (buf, "process %d", pid);
>> > else
>> >-    sprintf (buf, "thread %ld.0x%x", current_event.dwProcessId, pid);
>> >+    xasprintf (buf, "thread %ld.0x%x", current_event.dwProcessId, pid);
>> > return buf;
> 
> 
> As this is a static buffer, xasprintf can't be used here....
> Andrew, why are the target_pid_to_str functions supposed to return static buffers?
> Isn't that a big waste of memory?

Different coding styles.  People use static buffers (making the code 
non-reentrant) and sprintf() (making the code prone to buffer overruns) 
for a number of reasons.  One is that the programmer does know the 
lenght of the buffer and does know it won't be called re-entrantly so, 
rather than contend with cleanups, they use a static buffer.

Suggest adding a comment just above each sprintf() call indicating that 
buf is static (at least that way the next person won't be puzzled by this).

Andrew



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]