Skip to content

Conversation

sanjm25
Copy link

@sanjm25 sanjm25 commented Jul 7, 2025

Converted magic numbers to const.

@sanjm25 sanjm25 marked this pull request as draft July 7, 2025 22:33
@sanjm25 sanjm25 force-pushed the refactor/remove-magic-numbers branch from ef1d3c7 to 8cef9a9 Compare July 7, 2025 23:18
@sanjm25 sanjm25 marked this pull request as ready for review July 9, 2025 23:08
Copy link
Contributor

@leon-xd leon-xd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking through there is actually a decent amount of magic numbers that were missed -- I started logging all of them in queue.rs but kept finding more. think it's worth to just call that out and for you to manually check by hand whether there are magic numbers that you missed.

@@ -153,9 +158,9 @@ Exit the app anytime by pressing Ctrl-C

h.join().unwrap().unwrap();
} else {
perform_write_read_test(h_device, 512)?;
perform_write_read_test(h_device, u32::try_from(TEST_SIZE_SMALL).unwrap())?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this const not defined as the type it needs to be?


perform_write_read_test(h_device, 30 * 1024)?;
perform_write_read_test(h_device, u32::try_from(TEST_SIZE_LARGE).unwrap())?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing here switch to proper type

GLOBAL_BUFFER = ExAllocatePool2(POOL_FLAG_NON_PAGED, LENGTH as SIZE_T, 's' as u32);
GLOBAL_BUFFER = ExAllocatePool2(
POOL_FLAG_NON_PAGED,
POOL_ALLOCATION_SIZE as SIZE_T,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set correct type

@@ -79,7 +85,7 @@ fn echo_interlocked_increment_floor(target: &AtomicI32, floor: i32) -> i32 {
//
match target.compare_exchange(
current_value,
current_value + 1,
current_value + ATOMIC_INCREMENT_VALUE,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm does incrementing by 1 count as a magic number? suppose this is ok but given the lack of ++ operator i can imagine this could detract from readability rather than expand it

@@ -727,3 +733,7 @@ unsafe extern "C" fn echo_evt_timer_func(timer: WDFTIMER) {
}
}
}

// Define constants for magic numbers
const TOLERABLE_DELAY: u32 = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to put these with other constants should not be at bottom of file

@@ -162,7 +166,7 @@ extern "C" fn echo_evt_device_self_managed_io_start(device: WDFDEVICE) -> NTSTAT
// into low power state.
unsafe { call_unsafe_wdf_function_binding!(WdfIoQueueStart, queue) };

let due_time: i64 = -(100) * (10000);
let due_time: i64 = TIMER_DUE_TIME_MS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should just use TIMER_DUE_TIME_MS instead of due_time in the call to timer.start()

@@ -108,7 +112,7 @@ pub fn echo_device_create(mut device_init: &mut WDFDEVICE_INIT) -> NTSTATUS {
// it will return NULL and assert if run under framework verifier mode.
let device_context: *mut DeviceContext =
unsafe { wdf_object_get_device_context(device as WDFOBJECT) };
unsafe { (*device_context).private_device_data = 0 };
unsafe { (*device_context).private_device_data = DEFAULT_PRIVATE_DATA };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the type of private_device_data? doesn't feel like this should be expressed as a number

@@ -266,7 +272,7 @@ fn echo_decrement_request_cancel_ownership_count(request_context: *mut RequestCo
.fetch_sub(1, Ordering::SeqCst)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed a magic number here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also missed in echo_increment_request_cancel_ownership_count for some reason I can't write a comment there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and also echo_set_current_request

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually two of them in echo_set_current_request **

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there were too many to log in this comment chain so I submitted the review. Please check by hand for magic numbers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants