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