library: help ensure 'escape_str' isn't executed twice

Now that the ps program is using 'escape_str' for most
of the library's returned strings, this patch tries to
lessen the prospects of executing that function twice.

Our newlib branch has achieved such a goal through the
elimination of nearly all escape.c code. However, here
we avoid API change by trading some 'escape_str' calls
(with wide character overhead) for a slightly extended
'escaped_copy' call (which incurs no multibyte costs).

Note: until we migrate to the newlib version, there is
a remaining call to 'escape_str' which we can't avoid.
Such code involves the 'escape_command' function call.

[ As we prepare for this new (final?) release, there ]
[ were already internal library changes that require ]
[ a new 'revision'. This patch won't impact the API! ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner
2020-12-26 00:00:00 -06:00
committed by Craig Small
parent c76144ead1
commit eea5626bb1
2 changed files with 11 additions and 5 deletions

View File

@ -222,10 +222,10 @@ int escape_command(char *restrict const outbuf, const proc_t *restrict const pp,
/////////////////////////////////////////////////
// copy an already 'escaped' string,
// copy a string, but 'escape' any control characters
// using the traditional escape.h calling conventions
int escaped_copy(char *restrict dst, const char *restrict src, int bufsize, int *maxroom){
int n;
int i, n;
SECURE_ESCAPE_ARGS(dst, bufsize, *maxroom);
if (bufsize > *maxroom+1) bufsize = *maxroom+1;
@ -236,6 +236,12 @@ int escaped_copy(char *restrict dst, const char *restrict src, int bufsize, int
return 0;
}
if (n >= bufsize) n = bufsize-1;
// control chars, especially tabs, create alignment problems for ps & top ...
for (i = 0; i < n; i++)
if ((unsigned char)dst[i] < 0x20 || dst[i] == 0x7f)
dst[i] = '?';
*maxroom -= n;
return n;
}