On 2016-08-06 at 01:44:42 +0200, Vadim Kochan <vadi...@gmail.com> wrote: > On Fri, Aug 05, 2016 at 09:26:24AM +0200, Tobias Klauser wrote: > > On 2016-07-26 at 21:35:11 +0200, Vadim Kochan <vadi...@gmail.com> wrote: > > > +static inline unsigned int field_rand(struct proto_field *field) > > > +{ > > > + return field->func.min + (rand() % ((field->func.max - field->func.min) > > > + 1)); > > > +} > > > + > > > +static void field_rnd_func(struct proto_field *field) > > > +{ > > > + unsigned int val = field_rand(field); > > > + > > > + if (field->len == 1) { > > > + proto_field_set_u8(field->hdr, field->id, (uint8_t) val); > > > + } else if (field->len == 2) { > > > + proto_field_set_be16(field->hdr, field->id, (uint16_t) val); > > > + } else if (field->len == 4) { > > > + proto_field_set_be32(field->hdr, field->id, (uint32_t) val); > > > + } else if (field->len > 4) { > > > + uint8_t *bytes = __proto_field_get_bytes(field); > > > + uint32_t i; > > > + > > > + for (i = 0; i < field->len; i++, val = field_rand(field)) > > > + bytes[i] = (uint8_t) val; > > > > No need for the assigment of `val' in the loop, should rather be like > > this: > > > > for (i = 0; i < field->len; i++) > > bytes[i] = (uint8_t) field_rand(field); > > > + } > > I did so because field_rand(field) was calculated at the beginning so in > the loop it will be calculated only on the next round. Do you think it > is not needed ?
IMO it's easy to overread the val assignment in the for statement as it is not such a common pattern. We'd not even save the call to field_rand(), so I think it's worth to have this explicitely stated in the loop body rather than the for(...) statement. -- You received this message because you are subscribed to the Google Groups "netsniff-ng" group. To unsubscribe from this group and stop receiving emails from it, send an email to netsniff-ng+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.