Skip to content

Commit b62dbd7

Browse files
committed
lib: avoided increments/decrements with side effects
- moved ++/-- before or after the value use Signed-off-by: frei tycho <[email protected]>
1 parent 7f0f0a4 commit b62dbd7

File tree

5 files changed

+114
-64
lines changed

5 files changed

+114
-64
lines changed

lib/crc/crc16_sw.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ uint16_t crc16_ccitt(uint16_t seed, const uint8_t *src, size_t len)
5454
for (; len > 0; len--) {
5555
uint8_t e, f;
5656

57-
e = seed ^ *src++;
57+
e = seed ^ *src;
58+
++src;
5859
f = e ^ (e << 4);
5960
seed = (seed >> 8) ^ ((uint16_t)f << 8) ^ ((uint16_t)f << 3) ^ ((uint16_t)f >> 4);
6061
}
@@ -66,7 +67,8 @@ uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len)
6667
{
6768
for (; len > 0; len--) {
6869
seed = (seed >> 8U) | (seed << 8U);
69-
seed ^= *src++;
70+
seed ^= *src;
71+
++src;
7072
seed ^= (seed & 0xffU) >> 4U;
7173
seed ^= seed << 12U;
7274
seed ^= (seed & 0xffU) << 5U;

lib/os/cbprintf_complete.c

Lines changed: 82 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ static inline const char *extract_specifier(struct conversion *conv,
515515
{
516516
bool unsupported = false;
517517

518-
conv->specifier = *sp++;
518+
conv->specifier = *sp;
519+
++sp;
519520

520521
switch (conv->specifier) {
521522
case SINT_CONV_CASES:
@@ -652,7 +653,8 @@ static inline const char *extract_conversion(struct conversion *conv,
652653
*/
653654
++sp;
654655
if (*sp == '%') {
655-
conv->specifier = *sp++;
656+
conv->specifier = *sp;
657+
++sp;
656658
return sp;
657659
}
658660

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

800-
*--bp = (lsv <= 9) ? ('0' + lsv)
802+
--bp;
803+
*bp = (lsv <= 9) ? ('0' + lsv)
801804
: upcase ? ('A' + lsv - 10) : ('a' + lsv - 10);
802805
value /= radix;
803806
} while ((value != 0) && (bps < bp));
@@ -906,23 +909,27 @@ static char *encode_float(double value,
906909
if (expo == BIT_MASK(EXPONENT_BITS)) {
907910
if (fract == 0) {
908911
if (isupper((unsigned char)c) != 0) {
909-
*buf++ = 'I';
910-
*buf++ = 'N';
911-
*buf++ = 'F';
912+
buf[0] = 'I';
913+
buf[1] = 'N';
914+
buf[2] = 'F';
915+
buf += 3;
912916
} else {
913-
*buf++ = 'i';
914-
*buf++ = 'n';
915-
*buf++ = 'f';
917+
buf[0] = 'i';
918+
buf[1] = 'n';
919+
buf[2] = 'f';
920+
buf += 3;
916921
}
917922
} else {
918923
if (isupper((unsigned char)c) != 0) {
919-
*buf++ = 'N';
920-
*buf++ = 'A';
921-
*buf++ = 'N';
924+
buf[0] = 'N';
925+
buf[1] = 'A';
926+
buf[2] = 'N';
927+
buf += 3;
922928
} else {
923-
*buf++ = 'n';
924-
*buf++ = 'a';
925-
*buf++ = 'n';
929+
buf[0] = 'n';
930+
buf[1] = 'a';
931+
buf[2] = 'n';
932+
buf += 3;
926933
}
927934
}
928935

@@ -942,19 +949,22 @@ static char *encode_float(double value,
942949
if (IS_ENABLED(CONFIG_CBPRINTF_FP_A_SUPPORT)
943950
&& (IS_ENABLED(CONFIG_CBPRINTF_FP_ALWAYS_A)
944951
|| conv->specifier_a)) {
945-
*buf++ = '0';
946-
*buf++ = 'x';
952+
buf[0] = '0';
953+
buf[1] = 'x';
954+
buf += 2;
947955

948956
/* Remove the offset from the exponent, and store the
949957
* non-fractional value. Subnormals require increasing the
950958
* exponent as first bit isn't the implicit bit.
951959
*/
952960
expo -= 1023;
953961
if (is_subnormal) {
954-
*buf++ = '0';
962+
*buf = '0';
963+
++buf;
955964
++expo;
956965
} else {
957-
*buf++ = '1';
966+
*buf = '1';
967+
++buf;
958968
}
959969

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

9921002
if (require_dp || (precision != 0)) {
993-
*buf++ = '.';
1003+
*buf = '.';
1004+
++buf;
9941005
}
9951006

9961007
/* Get the fractional value as a hexadecimal string, using x
@@ -1010,12 +1021,15 @@ static char *encode_float(double value,
10101021
* point.
10111022
*/
10121023
while ((spe - sp) < FRACTION_HEX) {
1013-
*--sp = '0';
1024+
--sp;
1025+
*sp = '0';
10141026
}
10151027

10161028
/* Append the leading significant "digits". */
10171029
while ((sp < spe) && (precision > 0)) {
1018-
*buf++ = *sp++;
1030+
*buf = *sp;
1031+
++buf;
1032+
++sp;
10191033
--precision;
10201034
}
10211035

@@ -1028,19 +1042,24 @@ static char *encode_float(double value,
10281042
}
10291043
}
10301044

1031-
*buf++ = 'p';
1045+
*buf = 'p';
1046+
++buf;
10321047
if (expo >= 0) {
1033-
*buf++ = '+';
1048+
*buf = '+';
1049+
++buf;
10341050
} else {
1035-
*buf++ = '-';
1051+
*buf = '-';
1052+
++buf;
10361053
expo = -expo;
10371054
}
10381055

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

10421059
while (sp < spe) {
1043-
*buf++ = *sp++;
1060+
*buf = *sp;
1061+
++buf;
1062+
++sp;
10441063
}
10451064

10461065
*bpe = buf;
@@ -1174,22 +1193,25 @@ static char *encode_float(double value,
11741193
if (decexp > 0) {
11751194
/* Emit the digits above the decimal point. */
11761195
while ((decexp > 0) && (digit_count > 0)) {
1177-
*buf++ = _get_digit(&fract, &digit_count);
1196+
*buf = _get_digit(&fract, &digit_count);
1197+
++buf;
11781198
decexp--;
11791199
}
11801200

11811201
conv->pad0_value = decexp;
11821202

11831203
decexp = 0;
11841204
} else {
1185-
*buf++ = '0';
1205+
*buf = '0';
1206+
++buf;
11861207
}
11871208

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

11951217
if ((decexp < 0) && (precision > 0)) {
@@ -1214,45 +1236,52 @@ static char *encode_float(double value,
12141236
* format, or if more digits are to follow.
12151237
*/
12161238
if (conv->flag_hash || (precision > 0)) {
1217-
*buf++ = '.';
1239+
*buf = '.';
1240+
++buf;
12181241
}
12191242
}
12201243

12211244
while ((precision > 0) && (digit_count > 0)) {
1222-
*buf++ = _get_digit(&fract, &digit_count);
1245+
*buf = _get_digit(&fract, &digit_count);
1246+
++buf;
12231247
precision--;
12241248
}
12251249

12261250
conv->pad0_pre_exp = precision;
12271251

12281252
if (prune_zero) {
12291253
conv->pad0_pre_exp = 0;
1230-
while (*--buf == '0') {
1231-
;
1232-
}
1254+
do {
1255+
--buf;
1256+
} while (*buf == '0');
12331257
if (*buf != '.') {
1234-
buf++;
1258+
++buf;
12351259
}
12361260
}
12371261

12381262
/* Emit the explicit exponent, if format requires it. */
12391263
if ((c == 'e') || (c == 'E')) {
1240-
*buf++ = c;
1264+
*buf = c;
1265+
++buf;
12411266
if (decexp < 0) {
12421267
decexp = -decexp;
1243-
*buf++ = '-';
1268+
*buf = '-';
1269+
++buf;
12441270
} else {
1245-
*buf++ = '+';
1271+
*buf = '+';
1272+
++buf;
12461273
}
12471274

12481275
/* At most 3 digits to the decimal. Spit them out. */
12491276
if (decexp >= 100) {
1250-
*buf++ = (decexp / 100) + '0';
1277+
*buf = (decexp / 100) + '0';
1278+
++buf;
12511279
decexp %= 100;
12521280
}
12531281

1254-
*buf++ = (decexp / 10) + '0';
1255-
*buf++ = (decexp % 10) + '0';
1282+
buf[0] = (decexp / 10) + '0';
1283+
buf[1] = (decexp % 10) + '0';
1284+
buf += 2;
12561285
}
12571286

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

13261355
while ((sp < ep) || ((ep == NULL) && *sp)) {
1327-
int rc = out((int)*sp++, ctx);
1356+
int rc = out((int)*sp, ctx);
1357+
++sp;
13281358

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

13751405
while (*fp != 0) {
13761406
if (*fp != '%') {
1377-
OUTC(*fp++);
1407+
OUTC(*fp);
1408+
++fp;
13781409
continue;
13791410
}
13801411

@@ -1782,11 +1813,13 @@ int z_cbvprintf_impl(cbprintf_cb out, void *ctx, const char *fp,
17821813
if (conv->specifier_a) {
17831814
/* Only padding is pre_exp */
17841815
while (*cp != 'p') {
1785-
OUTC(*cp++);
1816+
OUTC(*cp);
1817+
++cp;
17861818
}
17871819
} else {
17881820
while (isdigit((unsigned char)*cp) != 0) {
1789-
OUTC(*cp++);
1821+
OUTC(*cp);
1822+
++cp;
17901823
}
17911824

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

17991832
if (*cp == '.') {
1800-
OUTC(*cp++);
1833+
OUTC(*cp);
1834+
++cp;
18011835
/* Remaining padding is
18021836
* post-dp.
18031837
*/
@@ -1806,7 +1840,8 @@ int z_cbvprintf_impl(cbprintf_cb out, void *ctx, const char *fp,
18061840
}
18071841
}
18081842
while (isdigit((unsigned char)*cp) != 0) {
1809-
OUTC(*cp++);
1843+
OUTC(*cp);
1844+
++cp;
18101845
}
18111846
}
18121847

lib/os/cbprintf_packaged.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
335335
* reason for the post-decrement on fmt as it will be incremented
336336
* prior to the next (actually first) round of that loop.
337337
*/
338-
s = fmt--;
338+
s = fmt;
339+
--fmt;
339340
align = VA_STACK_ALIGN(char *);
340341
size = sizeof(char *);
341342
goto process_string;
@@ -766,7 +767,8 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
766767
return -ENOSPC;
767768
}
768769
/* store the pointer position prefix */
769-
*buf++ = pos;
770+
*buf = pos;
771+
++buf;
770772
}
771773
}
772774

@@ -794,7 +796,8 @@ int cbvprintf_package(void *packaged, size_t len, uint32_t flags,
794796
return -ENOSPC;
795797
}
796798
/* store the pointer position prefix */
797-
*buf++ = str_ptr_pos[i];
799+
*buf = str_ptr_pos[i];
800+
++buf;
798801
/* copy the string with its terminating '\0' */
799802
memcpy(buf, (uint8_t *)s, size);
800803
buf += size;
@@ -851,7 +854,8 @@ int cbpprintf_external(cbprintf_cb out,
851854
*/
852855
for (i = 0; i < s_nbr; i++) {
853856
/* Locate pointer location for this string */
854-
s_idx = *(uint8_t *)s++;
857+
s_idx = *(uint8_t *)s;
858+
++s;
855859
ps = (char **)(buf + s_idx * sizeof(int));
856860
/* update the pointer with current string location */
857861
*ps = s;

lib/os/printk.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ static int buf_char_out(int c, void *ctx_p)
9797
{
9898
struct buf_out_context *ctx = ctx_p;
9999

100-
ctx->buf[ctx->buf_count++] = c;
100+
ctx->buf[ctx->buf_count] = c;
101+
++ctx->buf_count;
101102
if (ctx->buf_count == CONFIG_PRINTK_BUFFER_SIZE) {
102103
buf_flush(ctx);
103104
}
@@ -226,15 +227,16 @@ struct str_context {
226227
static int str_out(int c, struct str_context *ctx)
227228
{
228229
if ((ctx->str == NULL) || (ctx->count >= ctx->max)) {
229-
ctx->count++;
230+
++ctx->count;
230231
return c;
231232
}
232233

233234
if (ctx->count == (ctx->max - 1)) {
234-
ctx->str[ctx->count++] = '\0';
235+
ctx->str[ctx->count] = '\0';
235236
} else {
236-
ctx->str[ctx->count++] = c;
237+
ctx->str[ctx->count] = c;
237238
}
239+
++ctx->count;
238240

239241
return c;
240242
}

0 commit comments

Comments
 (0)