From 7ead136575fe5f6a689bfaadc2e004bac9025f2a Mon Sep 17 00:00:00 2001 From: Eugene Syromyatnikov Date: Sun, 17 Sep 2017 04:57:37 +0200 Subject: [PATCH] ucopy: return string size in umovestr We return the size that includes \0 in order to preserve existing behaviour (return 0 when \0 haven't been seen, return positive number when it has been seen). * ucopy.c (umovestr_peekdata, umovestr): Return string length including \0 instead of 1 when \0 is found. --- ucopy.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ucopy.c b/ucopy.c index c3be4170..2d9ffb38 100644 --- a/ucopy.c +++ b/ucopy.c @@ -203,6 +203,7 @@ umovestr_peekdata(const int pid, kernel_ulong_t addr, unsigned int len, { unsigned int nread = 0; unsigned int residue = addr & (sizeof(long) - 1); + void *const orig_addr = laddr; while (len) { addr &= -sizeof(long); /* aligned address */ @@ -237,7 +238,7 @@ umovestr_peekdata(const int pid, kernel_ulong_t addr, unsigned int len, memcpy(laddr, &u.x[residue], m); while (residue < sizeof(long)) if (u.x[residue++] == '\0') - return 1; + return (laddr - orig_addr) + residue; residue = 0; addr += sizeof(long); laddr += m; @@ -252,8 +253,7 @@ umovestr_peekdata(const int pid, kernel_ulong_t addr, unsigned int len, * Like `umove' but make the additional effort of looking * for a terminating zero byte. * - * Returns < 0 on error, > 0 if NUL was seen, - * (TODO if useful: return count of bytes including NUL), + * Returns < 0 on error, strlen + 1 if NUL was seen, * else 0 if len bytes were read but no NUL byte seen. * * Note: there is no guarantee we won't overwrite some bytes @@ -289,8 +289,10 @@ umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len, int r = vm_read_mem(pid, laddr, addr, chunk_len); if (r > 0) { - if (memchr(laddr, '\0', r)) - return 1; + char *nul_addr = memchr(laddr, '\0', r); + + if (nul_addr) + return (nul_addr - laddr) + 1; addr += r; laddr += r; nread += r; -- 2.40.0