Skip to content

Commit

Permalink
lib: avoided increments/decrements with side effects
Browse files Browse the repository at this point in the history
- moved ++/-- before or after the value use

Signed-off-by: frei tycho <[email protected]>
  • Loading branch information
tychofrei02 authored and nashif committed Jun 21, 2024
1 parent cc37eac commit f17a67f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 64 deletions.
6 changes: 4 additions & 2 deletions lib/crc/crc16_sw.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ uint16_t crc16_ccitt(uint16_t seed, const uint8_t *src, size_t len)
for (; len > 0; len--) {
uint8_t e, f;

e = seed ^ *src++;
e = seed ^ *src;
++src;
f = e ^ (e << 4);
seed = (seed >> 8) ^ ((uint16_t)f << 8) ^ ((uint16_t)f << 3) ^ ((uint16_t)f >> 4);
}
Expand All @@ -66,7 +67,8 @@ uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len)
{
for (; len > 0; len--) {
seed = (seed >> 8U) | (seed << 8U);
seed ^= *src++;
seed ^= *src;
++src;
seed ^= (seed & 0xffU) >> 4U;
seed ^= seed << 12U;
seed ^= (seed & 0xffU) << 5U;
Expand Down
129 changes: 82 additions & 47 deletions lib/os/cbprintf_complete.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ static inline const char *extract_specifier(struct conversion *conv,
{
bool unsupported = false;

conv->specifier = *sp++;
conv->specifier = *sp;
++sp;

switch (conv->specifier) {
case SINT_CONV_CASES:
Expand Down Expand Up @@ -652,7 +653,8 @@ static inline const char *extract_conversion(struct conversion *conv,
*/
++sp;
if (*sp == '%') {
conv->specifier = *sp++;
conv->specifier = *sp;
++sp;
return sp;
}

Expand Down Expand Up @@ -797,7 +799,8 @@ static char *encode_uint(uint_value_type value,
do {
unsigned int lsv = (unsigned int)(value % radix);

*--bp = (lsv <= 9) ? ('0' + lsv)
--bp;
*bp = (lsv <= 9) ? ('0' + lsv)
: upcase ? ('A' + lsv - 10) : ('a' + lsv - 10);
value /= radix;
} while ((value != 0) && (bps < bp));
Expand Down Expand Up @@ -906,23 +909,27 @@ static char *encode_float(double value,
if (expo == BIT_MASK(EXPONENT_BITS)) {
if (fract == 0) {
if (isupper((unsigned char)c) != 0) {
*buf++ = 'I';
*buf++ = 'N';
*buf++ = 'F';
buf[0] = 'I';
buf[1] = 'N';
buf[2] = 'F';
buf += 3;
} else {
*buf++ = 'i';
*buf++ = 'n';
*buf++ = 'f';
buf[0] = 'i';
buf[1] = 'n';
buf[2] = 'f';
buf += 3;
}
} else {
if (isupper((unsigned char)c) != 0) {
*buf++ = 'N';
*buf++ = 'A';
*buf++ = 'N';
buf[0] = 'N';
buf[1] = 'A';
buf[2] = 'N';
buf += 3;
} else {
*buf++ = 'n';
*buf++ = 'a';
*buf++ = 'n';
buf[0] = 'n';
buf[1] = 'a';
buf[2] = 'n';
buf += 3;
}
}

Expand All @@ -942,19 +949,22 @@ static char *encode_float(double value,
if (IS_ENABLED(CONFIG_CBPRINTF_FP_A_SUPPORT)
&& (IS_ENABLED(CONFIG_CBPRINTF_FP_ALWAYS_A)
|| conv->specifier_a)) {
*buf++ = '0';
*buf++ = 'x';
buf[0] = '0';
buf[1] = 'x';
buf += 2;

/* Remove the offset from the exponent, and store the
* non-fractional value. Subnormals require increasing the
* exponent as first bit isn't the implicit bit.
*/
expo -= 1023;
if (is_subnormal) {
*buf++ = '0';
*buf = '0';
++buf;
++expo;
} else {
*buf++ = '1';
*buf = '1';
++buf;
}

/* If we didn't get precision from a %a specification then we
Expand Down Expand Up @@ -990,7 +1000,8 @@ static char *encode_float(double value,
bool require_dp = ((fract != 0) || conv->flag_hash);

if (require_dp || (precision != 0)) {
*buf++ = '.';
*buf = '.';
++buf;
}

/* Get the fractional value as a hexadecimal string, using x
Expand All @@ -1010,12 +1021,15 @@ static char *encode_float(double value,
* point.
*/
while ((spe - sp) < FRACTION_HEX) {
*--sp = '0';
--sp;
*sp = '0';
}

/* Append the leading significant "digits". */
while ((sp < spe) && (precision > 0)) {
*buf++ = *sp++;
*buf = *sp;
++buf;
++sp;
--precision;
}

Expand All @@ -1028,19 +1042,24 @@ static char *encode_float(double value,
}
}

*buf++ = 'p';
*buf = 'p';
++buf;
if (expo >= 0) {
*buf++ = '+';
*buf = '+';
++buf;
} else {
*buf++ = '-';
*buf = '-';
++buf;
expo = -expo;
}

aconv.specifier = 'i';
sp = encode_uint(expo, &aconv, buf, spe);

while (sp < spe) {
*buf++ = *sp++;
*buf = *sp;
++buf;
++sp;
}

*bpe = buf;
Expand Down Expand Up @@ -1174,22 +1193,25 @@ static char *encode_float(double value,
if (decexp > 0) {
/* Emit the digits above the decimal point. */
while ((decexp > 0) && (digit_count > 0)) {
*buf++ = _get_digit(&fract, &digit_count);
*buf = _get_digit(&fract, &digit_count);
++buf;
decexp--;
}

conv->pad0_value = decexp;

decexp = 0;
} else {
*buf++ = '0';
*buf = '0';
++buf;
}

/* Emit the decimal point only if required by the alternative
* format, or if more digits are to follow.
*/
if (conv->flag_hash || (precision > 0)) {
*buf++ = '.';
*buf = '.';
++buf;
}

if ((decexp < 0) && (precision > 0)) {
Expand All @@ -1214,45 +1236,52 @@ static char *encode_float(double value,
* format, or if more digits are to follow.
*/
if (conv->flag_hash || (precision > 0)) {
*buf++ = '.';
*buf = '.';
++buf;
}
}

while ((precision > 0) && (digit_count > 0)) {
*buf++ = _get_digit(&fract, &digit_count);
*buf = _get_digit(&fract, &digit_count);
++buf;
precision--;
}

conv->pad0_pre_exp = precision;

if (prune_zero) {
conv->pad0_pre_exp = 0;
while (*--buf == '0') {
;
}
do {
--buf;
} while (*buf == '0');
if (*buf != '.') {
buf++;
++buf;
}
}

/* Emit the explicit exponent, if format requires it. */
if ((c == 'e') || (c == 'E')) {
*buf++ = c;
*buf = c;
++buf;
if (decexp < 0) {
decexp = -decexp;
*buf++ = '-';
*buf = '-';
++buf;
} else {
*buf++ = '+';
*buf = '+';
++buf;
}

/* At most 3 digits to the decimal. Spit them out. */
if (decexp >= 100) {
*buf++ = (decexp / 100) + '0';
*buf = (decexp / 100) + '0';
++buf;
decexp %= 100;
}

*buf++ = (decexp / 10) + '0';
*buf++ = (decexp % 10) + '0';
buf[0] = (decexp / 10) + '0';
buf[1] = (decexp % 10) + '0';
buf += 2;
}

/* Cache whether there's padding required */
Expand Down Expand Up @@ -1324,7 +1353,8 @@ static int outs(cbprintf_cb out,
size_t count = 0;

while ((sp < ep) || ((ep == NULL) && *sp)) {
int rc = out((int)*sp++, ctx);
int rc = out((int)*sp, ctx);
++sp;

if (rc < 0) {
return rc;
Expand Down Expand Up @@ -1374,7 +1404,8 @@ int z_cbvprintf_impl(cbprintf_cb out, void *ctx, const char *fp,

while (*fp != 0) {
if (*fp != '%') {
OUTC(*fp++);
OUTC(*fp);
++fp;
continue;
}

Expand Down Expand Up @@ -1782,11 +1813,13 @@ int z_cbvprintf_impl(cbprintf_cb out, void *ctx, const char *fp,
if (conv->specifier_a) {
/* Only padding is pre_exp */
while (*cp != 'p') {
OUTC(*cp++);
OUTC(*cp);
++cp;
}
} else {
while (isdigit((unsigned char)*cp) != 0) {
OUTC(*cp++);
OUTC(*cp);
++cp;
}

pad_len = conv->pad0_value;
Expand All @@ -1797,7 +1830,8 @@ int z_cbvprintf_impl(cbprintf_cb out, void *ctx, const char *fp,
}

if (*cp == '.') {
OUTC(*cp++);
OUTC(*cp);
++cp;
/* Remaining padding is
* post-dp.
*/
Expand All @@ -1806,7 +1840,8 @@ int z_cbvprintf_impl(cbprintf_cb out, void *ctx, const char *fp,
}
}
while (isdigit((unsigned char)*cp) != 0) {
OUTC(*cp++);
OUTC(*cp);
++cp;
}
}

Expand Down
12 changes: 8 additions & 4 deletions lib/os/cbprintf_packaged.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
* reason for the post-decrement on fmt as it will be incremented
* prior to the next (actually first) round of that loop.
*/
s = fmt--;
s = fmt;
--fmt;
align = VA_STACK_ALIGN(char *);
size = sizeof(char *);
goto process_string;
Expand Down Expand Up @@ -766,7 +767,8 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
return -ENOSPC;
}
/* store the pointer position prefix */
*buf++ = pos;
*buf = pos;
++buf;
}
}

Expand Down Expand Up @@ -794,7 +796,8 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
return -ENOSPC;
}
/* store the pointer position prefix */
*buf++ = str_ptr_pos[i];
*buf = str_ptr_pos[i];
++buf;
/* copy the string with its terminating '\0' */
memcpy(buf, (uint8_t *)s, size);
buf += size;
Expand Down Expand Up @@ -851,7 +854,8 @@ int cbpprintf_external(cbprintf_cb out,
*/
for (i = 0; i < s_nbr; i++) {
/* Locate pointer location for this string */
s_idx = *(uint8_t *)s++;
s_idx = *(uint8_t *)s;
++s;
ps = (char **)(buf + s_idx * sizeof(int));
/* update the pointer with current string location */
*ps = s;
Expand Down
10 changes: 6 additions & 4 deletions lib/os/printk.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ static int buf_char_out(int c, void *ctx_p)
{
struct buf_out_context *ctx = ctx_p;

ctx->buf[ctx->buf_count++] = c;
ctx->buf[ctx->buf_count] = c;
++ctx->buf_count;
if (ctx->buf_count == CONFIG_PRINTK_BUFFER_SIZE) {
buf_flush(ctx);
}
Expand Down Expand Up @@ -226,15 +227,16 @@ struct str_context {
static int str_out(int c, struct str_context *ctx)
{
if ((ctx->str == NULL) || (ctx->count >= ctx->max)) {
ctx->count++;
++ctx->count;
return c;
}

if (ctx->count == (ctx->max - 1)) {
ctx->str[ctx->count++] = '\0';
ctx->str[ctx->count] = '\0';
} else {
ctx->str[ctx->count++] = c;
ctx->str[ctx->count] = c;
}
++ctx->count;

return c;
}
Expand Down
Loading

0 comments on commit f17a67f

Please sign in to comment.