All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] create FLEX_ALLOC_MEM to FLEX_ALLOC_STR cocci rule
@ 2019-04-03 22:00 Denton Liu
  2019-04-03 22:00 ` [PATCH 1/2] midx.c: convert FLEX_ALLOC_MEM to FLEX_ALLOC_STR Denton Liu
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Denton Liu @ 2019-04-03 22:00 UTC (permalink / raw)
  To: Git Mailing List

I was playing around with coccinelle and I noticed that we have a use of
FLEX_ALLOC_MEM that could be converted into FLEX_ALLOC_STR. Convert it
and write a cocci rule to prevent this from happening.

Note that this was more of an exercise to teach myself how to use
coccinelle. I'm just submitting a useful patch that came as a result of
this. Please let me know if this is unwelcome.

Denton Liu (2):
  midx.c: convert FLEX_ALLOC_MEM to FLEX_ALLOC_STR
  cocci: FLEX_ALLOC_MEM to FLEX_ALLOC_STR

 contrib/coccinelle/flex_alloc.cocci | 13 +++++++++++++
 midx.c                              |  2 +-
 2 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 contrib/coccinelle/flex_alloc.cocci

-- 
2.21.0.834.geaa57a21fa


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] midx.c: convert FLEX_ALLOC_MEM to FLEX_ALLOC_STR
  2019-04-03 22:00 [PATCH 0/2] create FLEX_ALLOC_MEM to FLEX_ALLOC_STR cocci rule Denton Liu
@ 2019-04-03 22:00 ` Denton Liu
  2019-04-03 22:00 ` [PATCH 2/2] cocci: " Denton Liu
  2019-04-04  1:57 ` [PATCH 0/2] create FLEX_ALLOC_MEM to FLEX_ALLOC_STR cocci rule Jeff King
  2 siblings, 0 replies; 4+ messages in thread
From: Denton Liu @ 2019-04-03 22:00 UTC (permalink / raw)
  To: Git Mailing List

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 midx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/midx.c b/midx.c
index 8a505fd423..cb8190329a 100644
--- a/midx.c
+++ b/midx.c
@@ -70,7 +70,7 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local
 
 	midx_map = xmmap(NULL, midx_size, PROT_READ, MAP_PRIVATE, fd, 0);
 
-	FLEX_ALLOC_MEM(m, object_dir, object_dir, strlen(object_dir));
+	FLEX_ALLOC_STR(m, object_dir, object_dir);
 	m->fd = fd;
 	m->data = midx_map;
 	m->data_len = midx_size;
-- 
2.21.0.834.geaa57a21fa


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] cocci: FLEX_ALLOC_MEM to FLEX_ALLOC_STR
  2019-04-03 22:00 [PATCH 0/2] create FLEX_ALLOC_MEM to FLEX_ALLOC_STR cocci rule Denton Liu
  2019-04-03 22:00 ` [PATCH 1/2] midx.c: convert FLEX_ALLOC_MEM to FLEX_ALLOC_STR Denton Liu
@ 2019-04-03 22:00 ` Denton Liu
  2019-04-04  1:57 ` [PATCH 0/2] create FLEX_ALLOC_MEM to FLEX_ALLOC_STR cocci rule Jeff King
  2 siblings, 0 replies; 4+ messages in thread
From: Denton Liu @ 2019-04-03 22:00 UTC (permalink / raw)
  To: Git Mailing List

Ensure that a FLEX_MALLOC_MEM that uses 'strlen' for its 'len' uses
FLEX_ALLOC_STR instead, since these are equivalent forms.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 contrib/coccinelle/flex_alloc.cocci | 13 +++++++++++++
 1 file changed, 13 insertions(+)
 create mode 100644 contrib/coccinelle/flex_alloc.cocci

diff --git a/contrib/coccinelle/flex_alloc.cocci b/contrib/coccinelle/flex_alloc.cocci
new file mode 100644
index 0000000000..e9f7f6d861
--- /dev/null
+++ b/contrib/coccinelle/flex_alloc.cocci
@@ -0,0 +1,13 @@
+@@
+expression str;
+identifier x, flexname;
+@@
+- FLEX_ALLOC_MEM(x, flexname, str, strlen(str));
++ FLEX_ALLOC_STR(x, flexname, str);
+
+@@
+expression str;
+identifier x, ptrname;
+@@
+- FLEXPTR_ALLOC_MEM(x, ptrname, str, strlen(str));
++ FLEXPTR_ALLOC_STR(x, ptrname, str);
-- 
2.21.0.834.geaa57a21fa


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] create FLEX_ALLOC_MEM to FLEX_ALLOC_STR cocci rule
  2019-04-03 22:00 [PATCH 0/2] create FLEX_ALLOC_MEM to FLEX_ALLOC_STR cocci rule Denton Liu
  2019-04-03 22:00 ` [PATCH 1/2] midx.c: convert FLEX_ALLOC_MEM to FLEX_ALLOC_STR Denton Liu
  2019-04-03 22:00 ` [PATCH 2/2] cocci: " Denton Liu
@ 2019-04-04  1:57 ` Jeff King
  2 siblings, 0 replies; 4+ messages in thread
From: Jeff King @ 2019-04-04  1:57 UTC (permalink / raw)
  To: Denton Liu; +Cc: Git Mailing List

On Wed, Apr 03, 2019 at 03:00:03PM -0700, Denton Liu wrote:

> I was playing around with coccinelle and I noticed that we have a use of
> FLEX_ALLOC_MEM that could be converted into FLEX_ALLOC_STR. Convert it
> and write a cocci rule to prevent this from happening.
> 
> Note that this was more of an exercise to teach myself how to use
> coccinelle. I'm just submitting a useful patch that came as a result of
> this. Please let me know if this is unwelcome.

Both look pretty reasonable to me. Obviously it's a pretty trivial
change, but that's sort of the point: automate these things away so we
don't have to think about them during review.

Now if you could figure out a way to point people to FLEX_ALLOC in the
first place when they are not currently using it, _that_ would be pretty
amazing. But I suspect it's a bit too complicated. :)

-Peff

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-04-04  1:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-03 22:00 [PATCH 0/2] create FLEX_ALLOC_MEM to FLEX_ALLOC_STR cocci rule Denton Liu
2019-04-03 22:00 ` [PATCH 1/2] midx.c: convert FLEX_ALLOC_MEM to FLEX_ALLOC_STR Denton Liu
2019-04-03 22:00 ` [PATCH 2/2] cocci: " Denton Liu
2019-04-04  1:57 ` [PATCH 0/2] create FLEX_ALLOC_MEM to FLEX_ALLOC_STR cocci rule Jeff King

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.