diff mbox series

[RFC,2/3] powerpc/ps3: refactor strncpy usage attempt 2

Message ID 20230811-strncpy-arch-powerpc-platforms-ps3-v1-2-301052a5663e@google.com (mailing list archive)
State Changes Requested
Headers show
Series powerpc/ps3: refactor strncpy usage | expand

Commit Message

Justin Stitt Aug. 11, 2023, 9:19 p.m. UTC
This approach tries to use `make_field` inside of `make_first_field`.
This comes with some weird implementation as to get the same result we
need to first subtract `index` from the `make_field` result whilst being
careful with order of operations. We then have to add index back.

The behavior should be the same but would love some comments on this.

Signed-off-by: Justin Stitt <justinstitt@google.com>

---
Note: I swapped the position of the two methods so as to not have to
forward declare `make_field`. This results in a weird diff here.
---
 arch/powerpc/platforms/ps3/repository.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

Comments

Kees Cook Aug. 14, 2023, 11:13 p.m. UTC | #1
On Fri, Aug 11, 2023 at 09:19:20PM +0000, Justin Stitt wrote:
> This approach tries to use `make_field` inside of `make_first_field`.
> This comes with some weird implementation as to get the same result we
> need to first subtract `index` from the `make_field` result whilst being
> careful with order of operations. We then have to add index back.

I think for readability, it's better to avoid the function composition.
The index subtraction undoes the earlier addition -- I say just leave it
separate.

i.e. I like option 1 of 3 the best.

-Kees
diff mbox series

Patch

diff --git a/arch/powerpc/platforms/ps3/repository.c b/arch/powerpc/platforms/ps3/repository.c
index 1abe33fbe529..6b731a5d4adc 100644
--- a/arch/powerpc/platforms/ps3/repository.c
+++ b/arch/powerpc/platforms/ps3/repository.c
@@ -63,36 +63,33 @@  static void _dump_node(unsigned int lpar_id, u64 n1, u64 n2, u64 n3, u64 n4,
 }
 
 /**
- * make_first_field - Make the first field of a repository node name.
- * @text: Text portion of the field.
+ * make_field - Make subsequent fields of a repository node name.
+ * @text: Text portion of the field.  Use "" for 'don't care'.
  * @index: Numeric index portion of the field.  Use zero for 'don't care'.
  *
- * This routine sets the vendor id to zero (non-vendor specific).
  * Returns field value.
  */
 
-static u64 make_first_field(const char *text, u64 index)
+static u64 make_field(const char *text, u64 index)
 {
 	u64 n = 0;
 
 	memcpy((char *)&n, text, strnlen(text, sizeof(n)));
-	return PS3_VENDOR_ID_NONE + (n >> 32) + index;
+	return n + index;
 }
 
 /**
- * make_field - Make subsequent fields of a repository node name.
- * @text: Text portion of the field.  Use "" for 'don't care'.
+ * make_first_field - Make the first field of a repository node name.
+ * @text: Text portion of the field.
  * @index: Numeric index portion of the field.  Use zero for 'don't care'.
  *
+ * This routine sets the vendor id to zero (non-vendor specific).
  * Returns field value.
  */
 
-static u64 make_field(const char *text, u64 index)
+static u64 make_first_field(const char *text, u64 index)
 {
-	u64 n = 0;
-
-	memcpy((char *)&n, text, strnlen(text, sizeof(n)));
-	return n + index;
+	return PS3_VENDOR_ID_NONE + ((make_field(text, index) - index) >> 32) + index;
 }
 
 /**