提交 d8905b03 编写于 作者: R robottoy

fix updater crash due to optimization

Issue:I7I9X5
Test:libc-test, benchmark
Signed-off-by: Nrobottoy <wangyaofeng.wang@huawei.com>
Change-Id: I55f79a8bec9dc890121ec4fea10c982328c2073e
Signed-off-by: Nrobottoy <wangyaofeng.wang@huawei.com>
上级 e7a4942b
......@@ -2,7 +2,6 @@
#include <string.h>
#define MIN(a,b) ((a)<(b) ? (a) : (b))
char *fgets(char *restrict s, int n, FILE *restrict f)
{
char *p = s;
......@@ -15,42 +14,35 @@ char *fgets(char *restrict s, int n, FILE *restrict f)
if (n--<=1) {
f->mode |= f->mode-1;
FUNLOCK(f);
if (n) {
return NULL;
}
*s = '\0';
if (n) return 0;
*s = 0;
return s;
}
while (n) {
if (f->rpos == f->rend && __fill_buffer(f)) {
if (p == s || !feof(f)) {
s = 0;
}
break;
if (f->rpos != f->rend) {
z = memchr(f->rpos, '\n', MIN(f->rend - f->rpos, n));
k = z ? z - f->rpos + 1 : f->rend - f->rpos;
k = MIN(k, n);
memcpy(p, f->rpos, k);
f->rpos += k;
p += k;
n -= k;
if (z || !n) break;
}
/* search minimal length */
k = MIN(f->rend - f->rpos, n);
z = memchr(f->rpos, '\n', k);
if (z != NULL) {
k = z - f->rpos + 1;
}
memcpy(p, f->rpos, k);
f->rpos += k;
p += k;
n -= k;
if (z || !n) {
if ((c = getc_unlocked(f)) < 0) {
if (p==s || !feof(f)) s = 0;
break;
}
n--;
if ((*p++ = c) == '\n') break;
}
if (s) {
*p = 0;
}
if (s) *p = 0;
FUNLOCK(f);
return s;
}
weak_alias(fgets, fgets_unlocked);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册