Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle layer's negative top or left #46

Closed
wants to merge 7 commits into from
Closed

Conversation

namse
Copy link
Contributor

@namse namse commented May 18, 2023

closes #45

Other little code changes by rust-analyzer and rustfmt automatically.

@namse
Copy link
Contributor Author

namse commented May 18, 2023

Hmm. the problem is very complex here.

rgba_idx could be negative if the layer's top and left is negative. It's not considered in code.

@namse
Copy link
Contributor Author

namse commented May 18, 2023

@chinedufn
I think you have to make a decision about the design of rgba_idx.

Here are my suggestion,

  1. rgba_idx returns signed integer and user check it's negative
  2. rgba_idx returns Option<usize> to prevent negative index

@namse
Copy link
Contributor Author

namse commented May 18, 2023

I put the change that fn rgba_idx(...) returns Option<usize> at commit e4a008b.

@namse namse changed the title Check overflow on casting 'ixx as usize' Handle layer's negative top or left May 18, 2023
Comment on lines -77 to -80
if pixel_left < layer.layer_properties.layer_left as usize
|| pixel_left > layer.layer_properties.layer_right as usize
|| pixel_top < layer.layer_properties.layer_top as usize
|| pixel_top > layer.layer_properties.layer_bottom as usize
Copy link
Contributor Author

Choose a reason for hiding this comment

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

layer_left, right, top, bottom could be negative, so pixel_left and top should be casted from usize to i32, not casting i32 to usize.

@@ -203,7 +209,7 @@ fn rle_decompress(bytes: &[u8]) -> Vec<u8> {
} else {
let repeat = 1 - header;
let byte = cursor.read_1()[0];
for _ in 0..repeat as usize {
for _ in 0..repeat {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this kind of meaningless as usize codes.

@@ -32,16 +32,16 @@ pub enum ImageDataSectionError {
#[derive(Debug)]
pub struct ImageDataSection {
/// The compression method for the image.
pub(in crate) compression: PsdChannelCompression,
pub(crate) compression: PsdChannelCompression,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This changed by rustfmt and rust-analzyer automatically.

@@ -161,7 +161,7 @@ impl ImageDataSection {
let (red_start, red_end) =
(channel_data_start, channel_data_start + red_byte_count);

let red = bytes[red_start as usize..red_end as usize].into();
let red = bytes[red_start..red_end].into();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these guys are already usize.

Comment on lines 433 to 437
let left_in_layer = idx as u16 % self.width();
let left_in_psd = self.layer_properties.layer_left + left_in_layer as i32;

let top_in_psd = idx / self.width() as usize + self.layer_properties.layer_top as usize;
let top_in_layer = idx as u16 / self.width();
let top_in_psd = self.layer_properties.layer_top + top_in_layer as i32;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I casted unsigned integer to signed integer,
previously this casted signed integer to unsigned integer, which can occur overflow.

@chinedufn
Copy link
Owner

chinedufn commented May 19, 2023

Thanks for working on this! Will review this over the weekend.

I haven't looked at the code yet, but if you haven't already please add test(s) for the things that you fixed.

These tests should have failed before your fixes and pass after your fixes.

@namse
Copy link
Contributor Author

namse commented May 19, 2023

I see, I have a psd file that makes error with previous code. Give me some time to put test. :)

@kevzettler kevzettler mentioned this pull request Jan 3, 2024
@kevzettler
Copy link
Contributor

kevzettler commented Jan 3, 2024

@namse I created a PR to add tests to your branch namse#1

@chinedufn this PR seems to fix few issues #45 #26 #43 and would be good to get in for a release

Copy link
Owner

@chinedufn chinedufn left a comment

Choose a reason for hiding this comment

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

Thanks! Just need to land @kevzettler 's tests then I can merge this.

@kevzettler
Copy link
Contributor

@namse @chinedufn I have created a new PR at #52 which contains all the changes from this PR with the test cases. We can close this in favor of #52

@chinedufn chinedufn closed this Jan 4, 2024
chinedufn pushed a commit that referenced this pull request Jan 4, 2024
This PR is a extension of:
#46

This PR includes all the code from #46 and additionally has tests and README updates
@chinedufn chinedufn reopened this Jan 4, 2024
@chinedufn chinedufn closed this Jan 4, 2024
chinedufn pushed a commit that referenced this pull request Jan 4, 2024
This PR is a extension of:
#46

This PR includes all the code from #46 and additionally has tests and README updates

Co-authored-by: namse <skatpgusskat@naver.com>
@chinedufn
Copy link
Owner

Added a commit with both of you as co authors a57f32f

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.

No overflow protection for i32 as usize in this crate
3 participants